Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

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. radiant_Light203 1166 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

Answer this question