-- here is where i get confused
local str = "hi"
if not str == "hi" then
print("str doesnt equal hi")
else print("str does equal hi")
end
then this
local str = "hello"
if str ~= "hello" then
print("str does not equal hello")
else print("str does equal hello")
end
Why is the output different?
posatta
(pasta)
May 11, 2020, 4:05am
#2
This is an issue with your order of operations. not x == y
will check if not x
(which evaluates to false) is equal to y
, rather than if x isn’t equal to y.
2 Likes
posatta
(pasta)
May 11, 2020, 4:08am
#4
Yeah, that’ll work. You’re probably better off using ~= anyways, though, just because it’s the intended way to do it.
2 Likes
Ok, thank you. (30 characters)
1 Like
Scottifly
(Scottifly)
May 11, 2020, 4:18am
#6
local str = “hi”
if not str == “hi” then
print(“str doesnt equal hi”)
else print(“str does equal hi”)
end
local STR = “hello”
if STR ~= “hello” then
print(“STR does not equal hello”)
else print(“STR does equal hello”)
end
Put these into a single script and ran it (changed the second variable to STR) and got this:
str does equal hi
STR does equal hello
I get the same results even if I leave the second variable as ‘str’.
So how are you getting ‘different results’?
posatta
(pasta)
May 11, 2020, 4:23am
#7
If he changes str
to anything besides “hi”, it’ll still print that it does equal it. For example, “hey” is clearly not equal to “hi”, but here’s what it prints when str
is “hey”:
2 Likes
Scottifly
(Scottifly)
May 11, 2020, 4:27am
#8
Okay, now I gotcha.
(30 characters)