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

What is the point of putting local? instead of no local [closed]

Asked by 6 years ago

This question already has an answer here:

"local" in front of variables

Since after you put an end you can't use local what is the point

like for example

local part = game.workspace.part
end

--cannot use anymore


--new script
part = game.workspace.part
end
part.BrickColor = BrickColor.new("Lavender")
--I dont get why people use local in some of the scripts

0
You should *always* use local variables. Usage of global variables is bad practice, it makes code messy. Global variables also pollute the global namespace as well as increase risks for name collisions. User#19524 175 — 6y

Marked as Duplicate by RayCurse, Vulkarin, and User#19524

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?

1 answer

Log in to vote
1
Answered by 6 years ago

Here's a link to an page about Scopes Anyway's I don't remember all the reasons but here's an example why local variables might work better.

This works but however if I were to do this

local Function Test()
local Test = "Test"
wait(1)
end

local Function Test2()
local Test = "Test"
end

But if I do this then Test get's replaced with Test2.

local Function Test()
Test = "Test"
wait(1)
print(Test)
end

local Function Test_2()
Test = "Test2"
print(Test)
end
0
+1 (though it's "function" not "Function"). Further, note that you get a slight speed boost when accessing local variables (though just a few percent). Also, what you say about the variable 'Test' is a lot more impactful when you're in a large script and don't have the list of variables every function uses memorized -- you don't want to accidentally change a variable that you didn't mean to! chess123mate 5873 — 6y
0
Yeah was just making a example. casper123123123 357 — 6y
Ad