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,
01 | global_var = "Sandwiches" -- A global variable that can be accessed throughout this whole script. |
02 |
03 | function example() |
04 | print (global_var) -- Prints Sandwiches |
05 | local a = "Hello World" -- This variable is accessible inside of this function and only inside this function. |
06 | global_var = "Burgers" -- Set it to Burgers. |
07 | print (global_var) -- Prints Burgers |
08 | if a = = "Hello World" then |
09 | local b = 3 -- A variable that can only be accessed within this if statement, since that is the new scope. |
10 | end |
11 |
12 | print (a, b) -- Should print: Hello World nil |
13 |
14 | local function local_func() -- A function that can only be called within this function |
15 | print ( "Local function ran." ) |
Local variables vs. Global variables
Thanks,
Best regards,
IdealistDeveloper
There mostly isn't any difference in putting local and no local before a variable.
1 | local a = 1 |
2 | if a = = 1 then |
3 | b = 2 |
4 | end |
5 |
6 | print (b) --it'll print out 2 |
.
1 | local a = 1 |
2 | if a = = 1 then |
3 | local b = 2 |
4 | end |
5 |
6 | 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
01 | function urdad(e, h) |
02 | --var 1 cheese is local |
03 | local cheese = "Cheese" |
04 | --var 2 potassium is NOT local |
05 | potassium = banana |
06 | end |
07 |
08 | local function urmom() |
09 | print (cheese) |
10 | -- the above won't work since cheese is local to the urdad function and can only be called from there |
11 | print (potassium) |
12 | --this will work because potassium is global and can chance be called from anywhere |
13 | 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
1 | game.Workspace.Door.LocalScript.Disabled = true |
but with local u can do this
1 | local Door = game.Workspace.Door.LocalScript |
2 |
3 | Door.Disabled = true |
4 |
5 | wait( 1 ) |
6 |
7 | 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