Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Variables decision help?

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

✓ Accepted answer

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.

1 comment

0

Thanks so much! ;-)

This discussion is preserved as it was. New answers, comments, and votes are closed.