Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Use of local variables?

Local variables are supposed to be used within functions, but does it make any difference if the variable is local or not when it's at the top of the script before anything runs?

Also, if I use a do end, would that make any difference in performance if there's a nonlocal variable declared within it?

1 answer

✓ Accepted answer

Local variables are confined to the scope they were defined in. If a variable is defined in the global scope, it doesn't make any difference whether or not the variable is local or global. var = 5 and local var = 5 are the same if in the global scope. Actually, there is a small difference between the two in that accessing local variables is slightly faster then accessing global variables. In general, it is better to use local variables rather than global variables.

A do statement does not make any real performance impact and is used in order to make certain variables private and have a finer control over the scope of variables.

1 comment

0

What he said. Xd

This discussion is preserved as it was. New answers, comments, and votes are closed.