Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Difference between ~= and the not operator?

Asked by
par1d 19
4 years ago

Could someone clarify the difference between these two! I’m kind of confused of when these should be used and what is the difference?

1 answer

Log in to vote
0
Answered by
Elyzzia 1294 Moderation Voter
4 years ago
Edited 4 years ago

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

0
Not is an Boolean inversion operand. Also, In Canada, it is recognized as BEDMAS if you're confused @parld. Ziffixture 6913 — 4y
Ad

Answer this question