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

What is the point of putting local? instead of no local

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

1 comment

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.

1 answer

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

2 comments

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!

0

Yeah was just making a example.

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