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

What is difference between making a variable with local and without local?

Asked by 4 years ago

Title says it all. Example:

randomvariable = 100
local anothervariable = 100
0
A local variable can only be used in one function. If you were to use a variable you made in one function, it woudn't work anywhere else outside of that function. Sorukan 240 — 4y

3 answers

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

The difference in them is called scope. Where different things can be accessed.

For example, a local variable can only be used in the surrounding brackets.

A global variable can be used anywhere in the script, no matter where it's located.

e.g

local test1 = 1
test2 = 2

function hi()
    if test1 >= 1 then
        print("hi") --this will print
    end
    if test2 >= 1 then
        print("hi") --this will print
    end
end

in the example above, both of those will print. Let's see an example of where both won't.

fineVariable = 23
function badexample()
    local badVariable = 1
end

function test2()
    if badVariable == 1 then
        print(1) --this will not work, as it can't see the variable 'badVariable'
    end
    if fineVariable == 23 then
        print(23) --this will work, as the variable is global.
    end
end

As a further note, you must define the variable before using it.

if variable1 == 1 then
    ... --won't happen, as variable 1 doesn't exist yet.
end
variable1 = 1 

and for my last note, if you put the local variable at the top of the script, you can use it anywhere

local v1 = "hi"
local v2 = "bye"
local v3 = "no"

function hi1()
...
end

function h2()
...
end

function h3()
...
end

--You can use the top 3 variables wherever in the script, because you defined them at the top.

In all these examples I used functions, just because. This also applies to if statements, loops, etc. Any conditional statement scope applies. The wiki link I provide will explain more.

I believe I read somewhere that local variables are better, and in my personal experience I just always use local variables. I think it adds more clarification to what it actually is.

Here's the wiki article explaining this topic for further reading. roblox wiki

If you have any questions, just let me know! :)

Ad
Log in to vote
0
Answered by
RAFA1608 543 Moderation Voter
4 years ago

The difference of making a variable local or not local is that a local variable inside a scope (scopes look like these:)

numbertimes2 = 3
function waittimes2(number)
    local numbertimes2 = number + number --this variable can NOT be seen by the rest of the script outside this scope
    wait(numbertimes2)
    --inside here is a scope
end
waittimes2(2)
print(numbertimes2) --prints 3

but, if you make it like this:

numbertimes2 = 3
function waittimes2(number)
    numbertimes2 = number + number
    wait(numbertimes2)
end
waittimes2(2)
print(numbertimes2) --prints 4

local variables makes their "own" variable, therefore non-local variables change a variable if the variable with the same name exists example:

local number = 4
number = 3
print(number) --prints 3
--the same goes for normal variables (without local in it)
number2 = 4
number2 = 3
print(number2) --prints 3
--in case its inside a scope,
number3 = 4
do
    local number3 = 3
end
print(number3) --prints 4
--im terrible at explaining

i hope that helped

Log in to vote
0
Answered by
Geobloxia 251 Moderation Voter
4 years ago
variable = 5 -- variable exists

function onTouch()
    local othervariable = 5
    -- othervariable exists ONLY inside the function
    -- with variable also
end
script.Parent.Touched:Connect(onTouch)
-- variable only exists now

Answer this question