I was watching some scripting videos this morning and i don't know what local variables are this is how i do variables
part = script.Parent
and others do this
local part = script.Parent
Can someone tell me what the difference is and what local does?
I was watching some scripting videos this morning and i don't know what local variables are this is how i do variables
part = script.Parent
and others do this
local part = script.Parent
Can someone tell me what the difference is and what local does?
Local variables are basically, variables that run on it own scope. And local variables are good practice to maintain code for global variables
If you still don't understand click here
Also here's an practical example of using local variables
Scope
function createPart()
local part = Instance.new("Part")
end
This will run in its own scope. The way to tell wether it will happen in it own scope is to check whether there's a function of there s s function then that means it's going to happen in its own scope
Global Variables Global variables are variables that can be used anywhere of your script. They don't have to be accessed through a function Here's an practical example of how to use it
local part = script.Parent -- can be used anywhere in your script.
Make sure to accept my answer if it helped.
For example:
local part = script.Parent -- Even though it says local its actually global
part2 = workspace.Part2
Those are Global Variables. They can be used ANYWHERE in the script.
Local Variables:
function createPart()
local part = Instance.new("Part") -- This is a local variable. It can be used only within this function
end
Another Example:
if bool = true then
local bool2 = false -- Local Variable
end
Last Example:
part = script.Parent -- Global variable
function doSomething()
end
Hope this helps! Sorry if you can't understand.. It might be a little tricky for new scripters!
I think this will confuse him because he probably doesn't know what a local variable is so he might think that local variables are global variables.
still accepted, probably understands it
This discussion is preserved as it was. New answers, comments, and votes are closed.
3 comments
global variables can be used everywhere... If you put it in a function then you can use it outside the function, whereas if you put local variables in a function, you won't be able to use it from outside the function.
in a nutshell: local variables are variables that are not registered on the enviroment
bro you think he knows what an environment is?