Difference between not ==, and ~=?

-- 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?

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

So, this would fix it?

if not (x == y) then

end
1 Like

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

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’?

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”:
image

2 Likes

Okay, now I gotcha.
(30 characters)