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

When is using local acceptable?

Asked by
Resnex 60
10 years ago

For example, what is the difference between:

local Expl = Instance.New("Explosion", Workspace)

and

Expl = Instance.New("Explosion", Workspace)

2 answers

Log in to vote
5
Answered by
Destrings 406 Moderation Voter
10 years ago

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.

0
Mention scope, yo. adark 5487 — 10y
1
Also, read adark answer if you don't know what are they. I assumed you asked why people define them in the global scope. Destrings 406 — 10y
Ad
Log in to vote
2
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

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 ends. 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 ends. 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.

Answer this question