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

Why does if statement get passed when it shouldn't?

Asked by 7 years ago

I got a game with 4 different gamemodes, I want the game to run different lines of codes for gamemodes 'Skirmish', 'Free for all', 'Rushdown' then for the gamemode 'Juggernaut'. I used these lines to script the first 3 ones.

if foundgamemode == 'Skirmish' or 'Free for all' or 'Rushdown' then
    print(foundgamemode)
--then some other stuff here that isn't improtant

So the strange thing is that when the foundgamemode (just gamemode but I called it with just another name) == 'Juggernaut' then the script still runs the lines for the other 3 gamemodes for a reason. And when it prints the gamemode, the gamemode == 'Juggernaut', why is this happening, help!

3 answers

Log in to vote
3
Answered by 7 years ago
Edited 7 years ago

or will return the second argument if the first is falsey so foundgamemode == 'Skirmish' or 'Free for all' will have 'Free for all' returned if foundgamemode == 'Skirmish' is not true. More information here.

Only nil and false are considered as falsey more information here.

So when we would have a string returned ie:-

if 'Free for all' then
    print('ran')
end

this code above will be considered true and the code will be ran.

The solution is we need to compare each of these conditions instead of passing back a string e.g.

if foundgamemode == 'Skirmish' or foundgamemode == 'Free for all' or  foundgamemode =='Rushdown' then

    print('ran')
end

Edit:-

Just like in maths we run code in a particular order and some have a higher Precedence that others.

Example 2

-- using the following code we can break down how it will run
local data = 'data'

if data == 'a' or 'test' then
    -- code 
end

-- the above code condition printed out
print(data == 'a' or 'test') -- this will print test

-- first we will evaluate the '==' as it has the highest precedence 
-- this will result in
print(data == 'a') -- prints false

-- we next evaluate using the logical operator or, which as mentioned before returns its second arg if the first is false. From the previous step we have false so 'test' will be returned 
print(false or 'test') -- prints test

I hope this helps.

0
I don't understand WHY I used it wrong, as I don't understand your explanation, BUT I understand how I need to do that. Could you maybe re-explain why you can't use if = or or? VladimVladim 78 — 7y
0
I'm pretty sure he's saying that you can use | if (condition) or (condition) or (condition) then |. You just have to compare the string every single time you check it. Otherwise you're just saying | if 'Free for all' then | after the first 'or'. Troidit 253 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

You're using or incorrectly.

if foundgamemode =='sk' or foundgamemode =='ffa' or foundgamemode =='rd' then
    print(foundgamemode)
Log in to vote
0
Answered by 7 years ago

You used the statements wrong,

if foundgamemode == 'Skirmish' or 'Free for all' or 'Rushdown' ' then
    print(foundgamemode)

is the same as;

iffoundgamemode == 'Skirmish' or 'Free for all' == true or 'Rushdown' == true then

if you enter just a word, its going to assume you mean a variable. this here is another form of how you did it;

FFA = true
RD = true
if foundgamemode == 'Skirmish' or 'FFA' or 'RD' then

See Its seeing FFA or RD as a varuable becaue foundgamemode is only defined for the first statment.

You could fix this by doing so

fgm = foundgamemode
if fgm == 'Skirmish' or fgm ==  'FFA' or fgm == 'RD' then

Sorry for explaining this soo horribly but i hope it gets the point across.

0
Thanks for explaining, I totally understand it now, but I am accepting the other answer above just because they replied earlier, sorry, but thanks anyway ;) VladimVladim 78 — 7y
0
Ey, np I just wanted to get the point across, that's all that matters. Abstract_Life 65 — 7y

Answer this question