Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What are Local Variables?

Asked by
fifayy 28
4 years ago

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?

0
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. greatneil80 2647 — 4y
0
in a nutshell: local variables are variables that are not registered on the enviroment maumaumaumaumaumua 628 — 4y
0
bro you think he knows what an environment is? greatneil80 2647 — 4y

2 answers

Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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.

Ad
Log in to vote
0
Answered by
qVoided 221 Moderation Voter
4 years ago

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!

0
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. JesseSong 3916 — 4y
0
still accepted, probably understands it qVoided 221 — 4y

Answer this question