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
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.
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.
function createPart() local part = Instance.new("Part") -- This is a local variable. It can be used only within this function end
if bool = true then local bool2 = false -- Local Variable end
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!