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

Are local variables of the same name across different scripts considered one or more?

More of a general question than to do with scripting. But if I declare a local variable in a script, and lets say this script goes in a part that has 20 duplicates (i.e, there are 20 scripts with this local variable) would this count as 20 local variables or one?

I know that I could use CollectionService instead of this and as far as I know it would count as only 1 variable in that case; but this question is just for curiousity.

2 answers

✓ Accepted answer

Variables are only accessible/declared from the script they are initialized in (excluding _G). It will count as 20 different variables, each with a different value (whatever value they're assigned in that script).

_G lets you create global variables which can be accessed across different scripts with the same permissions (i.e. localscripts VS serverscripts).

An example:

-- Script 1
_G.variable = "Yo wassup";
print(_G.variable); -- 'Yo wassup'
wait(3);
print(_G.variable); -- 7
-- Script 2
wait(2);
print(_G.variable); -- 'Yo wassup'
_G.variable = 7;

Keep in mind that a global variable initialized in a localscript cannot be accessed by serverscript, and vice versa.

1 comment

0

Thank you, I'm worried about reaching the 200 local variable limit even though I'm trying to cut down on declaring too many useless variables.

Any local variable that you declare in a script is local to that particular script, correct.

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