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

1if foundgamemode == 'Skirmish' or 'Free for all' or 'Rushdown' then
2    print(foundgamemode)
3--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 8 years ago
Edited 8 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:-

1if 'Free for all' then
2    print('ran')
3end

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.

1if foundgamemode == 'Skirmish' or foundgamemode == 'Free for all' or  foundgamemode =='Rushdown' then
2 
3    print('ran')
4end

Edit:-

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

Example 2

01-- using the following code we can break down how it will run
02local data = 'data'
03 
04if data == 'a' or 'test' then
05    -- code
06end
07 
08-- the above code condition printed out
09print(data == 'a' or 'test') -- this will print test
10 
11-- first we will evaluate the '==' as it has the highest precedence
12-- this will result in
13print(data == 'a') -- prints false
14 
15-- 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
16print(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 — 8y
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 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

You're using or incorrectly.

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

You used the statements wrong,

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

is the same as;

1iffoundgamemode == '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;

1FFA = true
2RD = true
3if 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

1fgm = foundgamemode
2if 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 — 8y
0
Ey, np I just wanted to get the point across, that's all that matters. Abstract_Life 65 — 8y

Answer this question