Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

not vs ~= vs == ?

So all the parameters do the same thing in effect:

if not test then
end

if test ~= true then
end

if test == false then
end 

Is there any difference in the above code? They all do the exact same but I was wondering if there was one that was more 'efficient'.

2 answers

"not" and "~=" are needed for different jobs

So in short "not" inverts a Boolean where as "~=" is a comparison so e.g.

local abool = false
local atxt = "a"

-- not operator 
if not abool then 
--[code block] 
end

-- ~= operator
if atxt ~= "b" then 
--[code block] 
end

-- just some example for you to work out and to show how you can add combine them
if not (atxt  == "b") then
  --[code block] 
end

if  (not false) ==  (atxt ~= "b")  then
  --[code block]
 end

So as long an the logic is "true" then the code will run it does not matter how this is done.

Just some more info

Some languages ( I don't know if lua support this) allows the use of "===" which is a comparison foe the value and type of the object.

1 comment

0

Lua == is ===

All of your cases are actually different.

if not test then
	-- if test is nil or false
end

if test ~= true then
	-- if test is anything but true. This could be an empty string, or a Part
end

if test == false then
	-- if test is false. This is different to not test in that it doesn't allow nil
end

It's down to you to choose which one is right, and at that which makes sense.


Bonus round:
a ~= b is the same as not (a == b). Isn't that nice?

This discussion is preserved as it was. New answers, comments, and votes are closed.