For example, what is the difference between:
local Expl = Instance.New("Explosion", Workspace)
and
Expl = Instance.New("Explosion", Workspace)
Local variables are stored in the stack, instead of the data segment, that means they are accessed faster. Usually you set local in the global scope if the variable is going to be read and written a lot of times to improve performance.
local
is used as a scope delimiter.
Imagine your script is a box. This box contains lines of code, and other, smaller boxes, which each themselves contain lines of code and smaller boxes and so on. These boxes represent "blocks" of code delimited by lines that require end
s. For instance, if
and do
.
When a variable is not declared local at all, it is considered a "global" variable, not to be confused with variables in the Global table. It exists outside of the box structure, but can only be accessed by contents of the box. These "global" variables are not really useful in the ROBLOX environment, which is why it is suggested that all variables be declared using local where possible. (In certain environments, for instance LOVE iirc, "global" variables can be shared across certain "scripts". Don't quote me on that.)
When local
is used, the variable is considered inside of that box. Variables declared local inside one of the smaller boxes are only accessible within that box, which includes the boxes it contains. It is not usable outside of that box.
Technically, local variables stop existing one the "block" of code they are declared in end
s. This is called the variable "going out of scope". Variables declared using local
can have the same name as local or global variables from higher scopes. The lowest-scope variable possible takes precedence, but it does not overwrite the higher-scope version.
Here are some examples, for clarification if you need it:
--[[The typical case.]] x = "hi" do local x = "ho" print(x) --> ho end print(x) --> hi --[[Works when the first variable is local as well.]] local x = "hi" do local x = "ho" print(x) --> ho end print(x) --> hi --[[No locals.]] x = "hi" do x = "ho" print(x) --> ho end print(x) --> ho --[[First is local.]] local x = "hi" do x = "ho" print(x) --> ho end print(x) --> ho
In the first two examples, x
goes out of scope once the do
block ends, returning its original value.
In the third example, x
never goes out of scope (until the Script ends, that is).
In the fourth example, which is where local is really useful, x
does not go out of scope when the do
block ends, but it will go out of scope once the block that contains that code ends. In this specific case, it is identical to example 3, as there is no scope higher than the first x, so it persists until the script ends.
One of the key uses of this is so that you can freely use variable names in the code you write. In many cases, the variable x
is used multiple times throughout the script, often concurrently if events are utilized, holding different values for each case. local
lets this work by making each x
unique to each scope.