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.
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.
Any local variable that you declare in a script is local to that particular script, correct.