Error with if else statements that can't be fixed

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()
4 Likes

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()
2 Likes

I understand how your solution fixes the error, but could you explain to me why it would happen in the first place?

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.

Does this make sense?

1 Like

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?

It will still work, I’m just saying that’s the reason for the error. It’s pretty much just suggesting for a variable.

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)

2 Likes

The variables were initialized globally instead of within a scope, thus why var2 originally outputted cool. Take the below for example:

function a()
    var = 1
    local var2 = 2
end

print(var) -- nil
print(var2) -- nil
a()
print(var) -- 1
print(var2) -- nil

var was initialized globally while var2 was initialized within the scope of the function a, thus why var outputs 1 while var2 outputs nil.

Something similar with a function/event:

game.Players.PlayerAdded:Connect(OnPlayerAdded)

function OnPlayerAdded(NewPlayer)
    print("Hello", NewPlayer.Name, "!")
end
1 Like

Your “var = 2 and var = 1” things below the check function need to be above it, because the script reads from top to bottom.

2 Likes

I am not sure as to why this may happen, sorry for that.

2 Likes

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.

2 Likes

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.

Are you saying that it doesn’t matter where you use the variable as long as it’s declared globally somewhere in the script?

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

Yes, if a variable is declared globally (without the preceding “local” keyword) it can be referenced from anywhere from within the same script.

1 Like

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
1 Like

Would the reason why there’s an error be because an if else statement would count as 2 scopes even though it’s inside a single function?

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).