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

What does ~= do in Lua?... See it everywhere but still confused on what it does

Hello, I have a question. What does this do? ~=

Looks like an arithmetic operator, but not sure.

I've seen it used like this before.

local exampleVarriable = "Example"

if exampleVarriable ~= nil then
print("Something...")
end

4 answers

✓ Accepted answer

The ~= symbol means not equal

So basically saying

if exampleVarriable is not equal to nil then print("something...")

7 comments

0

I see, similar to !== in other OOP languages?

1

It's the same as != in C++.

0

!== is used in php

0

No, !== means not equal and not same type, like == means equals, but === means equals and type. So you would probably just do != in php unless you needed to check the type of a variable.

0

Your right!

0

Most languages don't have === and !== operators. C, C++, Java, C#, neither have it. == already behaves like that.

0

Typo. I meant !=

~= means not equal. For example, 1 ~= 2

If both numbers(or strings, or tables, etc.) are not equal to each other, it will return true. If both are equal, it will return false.

~= is Lua's inequality comparison operator.

a ~= b will evaluate to true if a and b are not equal. However if either a or b have a metatable with an __eq field set to a function then Lua will call that function instead. To avoid calling this function and perform a raw comparison, Lua provides a function call rawequal which compares a and b without invoking the __eq metamethod.

This last bit of information is really only of use to you if you make use of metatables, but I found it interesting to bring up anyway.

An interesting thing to note about equality comparisons is that {} == {} will never be true, as tables are compared by reference and not by value. This means that a variable holding a table will only ever be equal to another if the second refers to the same table in memory:

local t1 = {}
local t2 = {}

print(t1 == t2) -- false

t3 = t1

print(t1 == t3) -- true

Yet another thing to note is that NaN -- a special number constant meaning "Not a Number" and returned by undefined math operations -- equals nothing, not even itself.

local x = 0/0

print(x ~= x) -- true

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