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.
1 | if 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!
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:-
1 | if 'Free for all' then |
2 | print ( 'ran' ) |
3 | 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.
1 | if foundgamemode = = 'Skirmish' or foundgamemode = = 'Free for all' or foundgamemode = = 'Rushdown' then |
2 |
3 | print ( 'ran' ) |
4 | end |
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 |
02 | local data = 'data' |
03 |
04 | if data = = 'a' or 'test' then |
05 | -- code |
06 | end |
07 |
08 | -- the above code condition printed out |
09 | print (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 |
13 | print (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 |
16 | print ( false or 'test' ) -- prints test |
I hope this helps.
You're using or incorrectly.
1 | if foundgamemode = = 'sk' or foundgamemode = = 'ffa' or foundgamemode = = 'rd' then |
2 | print (foundgamemode) |
You used the statements wrong,
1 | if foundgamemode = = 'Skirmish' or 'Free for all' or 'Rushdown' ' then |
2 | print (foundgamemode) |
is the same as;
1 | 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;
1 | FFA = true |
2 | RD = true |
3 | 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
1 | fgm = foundgamemode |
2 | if fgm = = 'Skirmish' or fgm = = 'FFA' or fgm = = 'RD' then |
Sorry for explaining this soo horribly but i hope it gets the point across.