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

Variables decision help?

Asked by
Vividex 162
10 years ago

when do you use a local variable and when do you use a regular variable? Or does it just not matter which one you use, for example:

local work = game.Workspace

when would I use local

work = game.Workspace

and when would I just use a regular variable?

1 answer

Log in to vote
1
Answered by
Aethex 256 Moderation Voter
10 years ago

A local variable makes it, well, local to its scope. A scope is basically its area in the code or its block. For example,

function test()

    -- this is a code block

end

Using a local variable would make the value local to its own block. Another example is,

local test = true;

print(test);

if test then

    local test = false;

    print(test);

end

print(test);

If you tested that out in studio, then you would notice it prints true, false, then true again. You may also notice that nothing set the value to true again. Inside that if statement is a code block and it locally set the value to false. After the code block has ended, then so has the changes made.

0
Thanks so much! ;-) Vividex 162 — 10y
Ad

Answer this question