Could someone clarify the difference between these two! I’m kind of confused of when these should be used and what is the difference?
the not
operator tells you if a value isn't truthy (or is falsy), while the ~=
operator tells you if two values aren't equal
in lua, a value is truthy when it isn't nil and isn't false, but in other programming languages, values like 0 and "" are also not truthy
also, not
has "operator precedence" over ~=
, which means that not
is evaluated first
operator precedence is like the programming version of PEMDAS or BIDMAS, and you can find it here https://www.lua.org/pil/3.5.html
local foo = 1 local bar = false if not foo == bar then -- you might expect this to not print, but because "not foo" is evaluated first, it becomes false, since foo is a truthy value print("awesome! not foo equals bar!") end
sometimes, not
and ~=
accomplish totally different things
-- this VV local idiot = game:FindFirstChild("Workspace") if not idiot then print("idiot!") --> doesn't print else print("stupid") --> prints end -- is functionally the same as this VV local idiot = game:FindFirstChild("Workspace") if (idiot == nil or idiot == false) then print("idiot!") --> doesn't print else print("stupid") --> print end -- whereas this VV local idiot = game:FindFirstChild("Workspace") if idiot then -- putting a value just by itself checks if it is truthy print("idiot!") --> prints else print("stupid") --> doesn't print end -- is functionally the same as this VV local idiot = game:FindFirstChild("Workspace") if (idiot ~= nil and idiot ~= false) then print("idiot!") --> prints else print("stupid") --> doesn't print end