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

[SOLVED] What is the point of making variable "local"? (Read description)

Hey guys, today a simple question.

What is the point of making a "local" variable, when you can't use a variable in other scripts even if it global?

I know it might sound as a noob question, but I will be happy for any explanation, or an answer.

Thanks in advance!

3 comments

2

I believe it has to do with scopes

2

also, I think they are faster to read from when in the same scope as the thing calling it

1

brownpants01 is correct about them being faster (I think to read from and write to)

1 answer

✓ Accepted answer

A "local" Variable, is restricted to the enclosing function, If you wanted to not get confused with all your variables its quite useful.

Jim = "Jim"

function example()
	local Bob = "Bob"
	print(Bob)-- Prints "Bob"
	print(Jim)-- Prints "Jim"
end
example()
print(Jim) -- Prints "Jim"
print(Bob) -- Errors

5 comments

0

Please accept the answer if this helped :)

0

Line 10 would not error. It would print nil.

0

it also means that you can't share it through scripts, like _G

0

Wrong. local var = "hi"; _G.var = var; <-- what's stopping you from doing that @Time_URSS?

1

not just a function, any scope. loops, do blocks, if statements, etc

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