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

Do I have to use local for creating variables?

Asked by
Neon_N 104
6 years ago

https://www.youtube.com/watch?v=UVLXjwqOQKI (Link)

On the link youtuber just makes variable without using local.

Script

Particles = script.Parent.Parent.ParticlesPart.ParticlesEmitter

script.Parent.Touched:connect(function(Part)
    local IsPar = Part.Parent.UpperTorso:FindFirstChild("ParticleEmitter")

    if not IsPar then
       local particles = Particles:Clone()
       particles.parent = Part.Parent.UpperTorso

   else
       Part.Parent.UpperTorso.ParticlesEmitter:Destroy()
       local particles = Particles:Clone()
      particles.Parent = Part.Parent.UpperTorso

      end
  end)?

as you see on

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.

2
Because it's bad practice. A variable that is not local is by default a global variable. Global variables make your codebase a mess. There's also that negligible speed difference. Local variables are only slightly faster to access than global variables. User#24403 69 — 6y
2
And whoever youtuber that is, he/she is just teaching bad code. User#24403 69 — 6y
0
^ - and just so you know OP, all of this is documented under scope - a concept which is good to teach, but not well practiced in Lua. (https://developer.roblox.com/articles/Scope) SummerEquinox 643 — 6y
0
Actually the youtuber who is teaching bad practices is EppoBot, way to go lol. LoganboyInCO 150 — 6y
0
Actually the youtuber who is teaching bad practices is EppoBot, way to go lol. LoganboyInCO 150 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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:

a = 1
if a == 1 then
local b = a+1
print(a)
print(b)
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:

a = 1
c = 3
if a == 1 then
local b = 2
end
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:

local a = 1
local b = 2
local c = 3
if a ~= b then
a = a + 1
print(a)
print(b)
print(c)
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.

0
In an optimal world you would *always* use `local` variables. Globals shouldn't ever be used. Link150 1355 — 5y
Ad
Log in to vote
-3
Answered by 6 years ago

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:

num = 5 -- This is a global variable
bool = true -- Also a global variable
script.Parent.Touched:Connect(function()
    if bool then
        bool = false
        num = num + 1
    end
    wait(3)
    bool = true
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:

bool = true
script.Parent.Touched:Connect(function()
    if bool then
        bool = false
        local num = 1
        num = num + 1 -- "num" is still a local variable, don't worry
    end
    wait(3)
    bool = true
end)

So as you can see, local variables should ONLY be used within their code block. If you have any questions, please ask them.

Answer this question