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!
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.
You're using or incorrectly.
if foundgamemode =='sk' or foundgamemode =='ffa' or foundgamemode =='rd' then print(foundgamemode)
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.