Examples are like putting a local before a function, what happens if you do it? What happens if you don’t? When should I use local in scripts?
What is local used for? I am very confused and need examples of when you should use it.
5 answers
Hey Calvin,
In order to really understand the meaning of having a local in front of your variables, you need to understand how variables are stored in memory. There is a heap and a stack where variables are stored. When you declare a function local, it is stored in the stack and is scope-dependent. This means that a local variable can only be accessed within its scope. A global variable on the other hand has memory allocated to it from the heap and can be accessed anywhere in the script. So, let's move on to examples here.
####Local vs. Global
global_var = "Sandwiches" -- A global variable that can be accessed throughout this whole script.
function example()
print(global_var) -- Prints Sandwiches
local a = "Hello World" -- This variable is accessible inside of this function and only inside this function.
global_var = "Burgers" -- Set it to Burgers.
print(global_var) -- Prints Burgers
if a == "Hello World" then
local b = 3 -- A variable that can only be accessed within this if statement, since that is the new scope.
end
print(a, b) -- Should print: Hello World nil
local function local_func() -- A function that can only be called within this function
print("Local function ran.")
end
end
print(global_var) -- Prints Sandwiches
example() -- Initiates the above function.
print(global_var) -- Prints Burgers
local_func() -- Throws an error "attempt to call a nil value"
##As a general rule, local variables are better to use since they allow the variable to be stored only when it is relevant. Global variables are frowned upon usually since they do not follow common coding practice and generally tend to cause lag in the long run.
#Here are some resources I recommend.
Local variables vs. Global variables
Thanks,
Best regards,
IdealistDeveloper
There mostly isn't any difference in putting local and no local before a variable.
local a = 1
if a == 1 then
b = 2
end
print(b) --it'll print out 2
.
local a = 1
if a == 1 then
local b = 2
end
print(b) --it'll print out nil
As stated in the Variables Website here,
Local variables are obtained faster than global variables because they're integrated into the environment in which they were created. If possible, you should always use local variables over global variables, unless there's a specific reason otherwise.
2 comments
Scopes is one way to counter your argument, as well as scenarios of debugging and using setfenv and getfenv
this doesn't explain why b will be nil, make your answer explain scopes and local vs global
To understand this we have to go into the two types of publicity for variables Local vs Global
A local variable is a variable only accessible by the script or function that it is in, Globals can be accessed from anywhere, to make a global you simply just don't put local in front of a variable the script I have below should show you what I mean
function urdad(e, h)
--var 1 cheese is local
local cheese = "Cheese"
--var 2 potassium is NOT local
potassium = banana
end
local function urmom()
print(cheese)
-- the above won't work since cheese is local to the urdad function and can only be called from there
print(potassium)
--this will work because potassium is global and can chance be called from anywhere
end
The same applies for functions too, like if I was in a for loop and I made a local function I could only call it from the for loop
I hope that cleared up your doubts!
Its a variable, a way to refer to something stored in a variable
game.Workspace.Door.LocalScript.Disabled = true
but with local u can do this
local Door = game.Workspace.Door.LocalScript
Door.Disabled = true
wait(1)
Door.Disabled = false
its very useful when you want to refer to something multiple times
The function local is the same as var. If you use Javascript or any other coding, local is a way to make a variable. This can store data and can be changed. For example, ~~~~~~~~~~~~~~~~~
local Baseplate = game.Workspace.Baseplate Baseplate:Destroy()
Using a variable is easier to get data faster, or storing some
4 comments
this isnt near of a real explanation
This doesn't answer his question
this isn't an explanation nor an answer first of all, it's gibberish put together by a rookie programmer dealt in a childish explanation.
yea! basically a variable
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
local function use only in server script it will work same as local script it willmake something only for local player