Scoping in scripts

It’s got to have something to do with the for loop or the fact that all of that is a in a function of its own since I am using system.util.invokeAsynchronous and system.util.invokeLater