Title is pretty self explanatory.
—What we can’t do:
var = true
—What we can do:
local var = true
Title is pretty self explanatory.
—What we can’t do:
var = true
—What we can do:
local var = true
https://create.roblox.com/docs/luau/variables#name-variables
“Variables can have global or local scopes. They have global scope by default, but it’s almost always better to create them with local scope because Luau accesses local variables faster than global ones. To give a variable local scope, put the keyword local before a variable’s name when you assign a value to it. For more information on Scope in Luau, see Scope.”
Luau can declare variables with the following syntax:
x = -- y
This is the natural state of variables. They’re made globally accessible, meaning the scope it is defined in does not matter. If you’re referring to variables which can be accessed by other scripts, that is an awful practice. Any shared information should be restrained to a ModuleScript
Why is it an awful practice? character limjt
You can use _G
and shared
to create global variables between scripts in the same context but theres basically no good reason to do it since there are much better and safer ways of doing it.
Both _G
and shared
have the exact same purpose and use. But just because they are there doesn’t mean you should use them generally. In my opinion, the only safe case of using them is on the server as the client would be able to modify them i.e. exploiters.
Roblox, by default, does not guarantee the execution order of your scripts. This makes it highly likely that a script in your codebase may attempt to utilize a global variable that has yet to be initialized, or initialize it incorrectly, leading to several semantic or catastrophic failures. On that note, it is incredibly tedious to diagnose bugs related to global variables, as their true point of initialization is ambiguous. It also becomes difficult to annotate the data linked to that variable, making it even less safe to work with.
Generally, global data should be read-only, or controlled by one interface, which is achievable through ModuleScript
s
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.