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?
Hey Calvin,
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"
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.
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