As I have been working on my game, I have noticed that when doing if statements, scripts DO NOT like using multiple conditions using “or”
This statement works:
local pet1 = "FoxPet"
local pet2 = "DeerPet"
local pet3 = "WaterDragonPet"
Pet = --a variable that comes from another source, specifically, the FoxPet
if Pet ~= pet1 then
--do some stuff
else
--do some other stuff --it will perform this code
end
but this doesn’t when it is like this…
local pet1 = "FoxPet"
local pet2 = "DeerPet"
local pet3 = "WaterDragonPet"
Pet = --a variable that comes from another source, specifically, the FoxPet
if Pet ~= pet1 or Pet ~= pet2 or Pet ~= pet3 then
--do some stuff --it will perform this code
else
--do some other stuff
end
Does anyone know why this happens??? I am pretty sure I am doing it correctly… Please correct me, if I am wrong!
local pet1 = "FoxPet"
local pet2 = "DeerPet"
local pet3 = "WaterDragonPet"
Pet = --a variable that comes from another source, specifically, the FoxPet
if (Pet ~= pet1) or (Pet ~= pet2) or (Pet ~= pet3) then
--do some stuff --it will perform this code
else
--do some other stuff
end
If you’re trying to make sure “Pet” isn’t equal to “pet1” “pet2” and “pet3” you should use “and” not “or”, currently the part with --do some stuff --it will perform this code is ran regardless of what the value is since if it’s the same as “pet1” it wouldn’t be the same as “pet2”, etc, therefore the condition would be true.
If that’s the case, try this:
local pet1 = "FoxPet"
local pet2 = "DeerPet"
local pet3 = "WaterDragonPet"
Pet = --a variable that comes from another source, specifically, the FoxPet
if Pet ~= pet1 and Pet ~= pet2 and Pet ~= pet3 then
--do some stuff --it will perform this code
else
--do some other stuff
end
I don’t see the reason why you’d want to use “or” in general for this, as the first condition would always return true. It seems that you’re misunderstanding the way “or” works in this scenario. Grouping together conditions with “or” results in each condition being checked individually in order of them being written.
The only way the “else” part would run is if the “Pet” variable is equal to 3 other variables at the same time, which is impossible if the values differ.
In other words;
if Pet ~= pet1 or Pet ~= pet2 or Pet ~= pet3 then
would be the same as: if false or true or true then
or if true or false or true then
or if true or true or false then
Which in any case would be true, since “or” only requires one of the conditions to be true to pass. Even if the value of “Pet” was utter nonsense it’d still return true as such: if true or true or true then
If you want to make sure “Pet” doesn’t equal any of the 3, you have to use “and” as described above.
The “or” keyword checks if any of the conditions are true, and will continue if so. The “and” keyword makes sure all of the conditions are true, else it’ll not continue and jump to the else or skip the code entirely if none is present.
This is part of DeMorgan’s Boolean law, and I’d just like to compound on the above post with the equations and some cool visual representations behind this law, as its VERY useful in countless fields of math
This is basically what DeMorgans law states
To explain the weird formatting, a line above something means “NOT”
“+” means OR
“*” means AND
This is the typical formatting for AOI (And Or Inverse) logic
So what this is basically saying, in Lua format, is:
(not x) or (not y) == not(x and y)
(not x) and (not y) == not(x or y)
Let me show this visually, in terms of a Venn Diagram
Yeah that’s what I was trying to say, but you’ve done a much better job at explaining it with imagery, since primitive type variables can’t be multiple values at once it’d never return false. Hopefully that’ll clear it up for the OP.