Alright so basically, I was playing around with some if statements and came across this. The script works but studio keeps insisting that var2 should be set to local. But when I set var2 to local, studio no longer recognizes what it is and brings another error. Could somebody please explain what’s happening?
function check()
if var == 1 then
print(var2)
else
var2 = "cool"
end
end
var = 2
check()
var = 1
check()
You have to set a variable first in order to do that.
local var = 0 -- number value
local var2 = "" -- string value
function check()
if var == 1 then
print(var2)
else
var2 = "cool"
end
end
var = 2
check()
var = 1
check()
Yes. For your var function to work, you have to set an initial value to it first. When you don’t, it receives an error because the script has no idea what “var” and “var2” is, since there is not variable for it.
It does make sense, another question though, how come the original script works? Scripts are read from top to bottom so how come it was able to understand what var2 is?
Basically, there was no initial value set for “var2.” There was a value, but it was not initial.
In this case, scripts read variables for values as INITIAL value. In other words, starting values. The value you set var2 = "cool" is seen as a SET value, which is read as attempt to set and CHANGE the value, which it cannot do if there is not initial value.
Sorry I’m still unable to figure out how the original script worked from your explanation. Would this mean that you can find the value of a variable no matter where it is as long as it’s a set variable? Would global variables set the value and local variables create the initial value?
Ah, disregarding the error for a second though, would you happen to also have an explanation for the original script working? I’m curious to know how it still ran fine. (sorry if i’m taking a lot of your time, i really appreciate the responses)
Because if you declare “var2” locally, contained inside the scope of the function, like this:
function check()
if var == 1 then
print(var2)
else
local var2 = "cool"
end
end
var = 2
check()
var = 1
check()
var2 inside the function is considered a different variable to var2 outside of the function, when you don’t declare a variable (with “local” preceding it) the variable is declared globally and can be accessed anywhere within the script.
That wasn’t the issue, I just set var to 2 and 1 to run the if else statement in the function. The script itself also works fine, I wanted to know how the script was able to define what var2 was even though the variable was set below the print.
I understand that local variables can only be used within its corresponding function that it’s in, but I’m unsure of why my original script was able to use var2 when it hasn’t been defined yet
In your original program you didn’t define var2 to the conditional within the function’s scope. Applying local to it had it be defined to the conditional’s scope, unable to be used outside of it.
-- Main scope
function foo()
-- Second/function scope
if 2 + 2 == 4 then
-- Third/conditional scope
local var = 4
print(var) -- 4
end
print(var) -- nil
end
foo()
local x = 4
do
local x = 5
print(x) -- 5
end
print(x) -- 4
I think it may better explain in terms of the “life” of a variable/value-
local var1 = 5
The above variable (var1) with value 5 has a “lifetime” of until the end of the program, since it’s within the “main” scope of the program. Now, let’s write a local var2 with a value of 10 in a do scope-
local var1 = 5
do
local var2 = 10
end
print(var1) -- 5
print(var2) -- nil
var2 with a value of 10 now has a “lifetime” that lasts until the do block ends. However, if we take away the local from var2-
local var1 = 5
do
var2 = 10
end
print(var1) -- 5
print(var2) -- 10
var2 with a value of 10 now has a “lifetime” similar to var1, since it was not defined as a local variable within the do block.
TL;DR local sets the “lifetime” of a variable (more specifically the value that’s assigned to it) within a scope(s).