https://www.youtube.com/watch?v=UVLXjwqOQKI (Link)
On the link youtuber just makes variable without using local.
Script
01 | Particles = script.Parent.Parent.ParticlesPart.ParticlesEmitter |
02 |
03 | script.Parent.Touched:connect( function (Part) |
04 | local IsPar = Part.Parent.UpperTorso:FindFirstChild( "ParticleEmitter" ) |
05 |
06 | if not IsPar then |
07 | local particles = Particles:Clone() |
08 | particles.parent = Part.Parent.UpperTorso |
09 |
10 | else |
11 | Part.Parent.UpperTorso.ParticlesEmitter:Destroy() |
12 | local particles = Particles:Clone() |
13 | particles.Parent = Part.Parent.UpperTorso |
14 |
15 | end |
16 | end )? |
as you see on
1 | Particles = script.Parent.Parent.ParticlesPart.ParticlesEmitter |
Why does he not use local for making variables? And why do we have local if we can just create variables without local? Please answer.
local variables are used inside of a specific part of the script, while global variables, variables without the "local" prefix, can be used anywhere in the script.
example 1:
1 | a = 1 |
2 | if a = = 1 then |
3 | local b = a+ 1 |
4 | print (a) |
5 | print (b) |
6 | end |
variable 'a' is being used in the if condition and can be used anywhere in the script. whereas 'b' can only be used in the if condition since it is a local variable for that section of code. but what if we made 'b' a global variable? It wont change anything, but it will give a recommendation to change it to local.
example 2:
1 | a = 1 |
2 | c = 3 |
3 | if a = = 1 then |
4 | local b = 2 |
5 | end |
6 | print (b + c) |
line 6 would print nil (I'm not in studio at the moment and it might print something else) since 'b' is only accessible in the if condition.
example 3:
1 | local a = 1 |
2 | local b = 2 |
3 | local c = 3 |
4 | if a ~ = b then |
5 | a = a + 1 |
6 | print (a) |
7 | print (b) |
8 | print (c) |
9 | end |
a and b would print '2' and c would stay the same. since the local variables aren't trapped in anything, they can be used for any part of the script.
In essence, you do not have to use local variables, but are recommended to keep your code cleaner.
About that rumor that global variables are bad for your script? That is entirely false. Both global and local variables are defined in a script, but it's where you put them that matters. Global variables (variables defined without the keyword local) can be used anywhere in a script. They are usually put outside of loops and statements to prevent any bugs from occurring. For example:
01 | num = 5 -- This is a global variable |
02 | bool = true -- Also a global variable |
03 | script.Parent.Touched:Connect( function () |
04 | if bool then |
05 | bool = false |
06 | num = num + 1 |
07 | end |
08 | wait( 3 ) |
09 | bool = true |
10 | end ) |
Line 5 may be confusing because it reassigns a value to the variable, which happens to be the opposite boolean value, false. However, when reassigning a variable to a value, you can't change what kind of variable it is (if you do you will get an error). The variables num and bool are global and are used within the function. There are no errors because Lua sees that "Oh, this variable is global. We can use this wherever we want." It's really simple.
Local variables are a whole different story. You know about those scripts on Scripting Helpers that use local variables like global variables? It's not right, and the only reason why they're pulling it off is because Roblox rigs the purpose of local variables and makes them like global variables. Local variables should only be available within the code block it's assigned in. Example:
01 | bool = true |
02 | script.Parent.Touched:Connect( function () |
03 | if bool then |
04 | bool = false |
05 | local num = 1 |
06 | num = num + 1 -- "num" is still a local variable, don't worry |
07 | end |
08 | wait( 3 ) |
09 | bool = true |
10 | end ) |
So as you can see, local variables should ONLY be used within their code block. If you have any questions, please ask them.