Is it possible in Lua where "not 1" = 0 and vice versa?

Hi there, I know in C, using != operator with 1 would return 0, using it with 0 would return 1. Is this possible in Lua-U (or at least vanilla) such as how not true would equal false and vice versa?

1 Like

No, since the not operator cannot be overloaded using metamethods and besides you can’t use metatables at all on any type other than table (like numbers) . Are you porting C code that needs this kind of behavior or is it just a matter of how you’re used to working?

Nope. Use this instead:

local x = 1
print(1 - x) -- 1 turns to 0, 0 turns to 1
1 Like

Nope. Numbers and booleans are separate concepts in Lua (and Luau by extension). In Lua 1 ~= true, however you can use the ~= operator to check if something is not equal.

You can also use if-then-else or ternary-like comparisons to switch between the two; e.g.

local x = num == 1 and 0 or 1
local y = if num == 1 then 0 else 1

1 - 0 is 1,
1 - 1 is 0.
It’s just math that does the work.

1 Like

in C# it is:

1 != 0

I Lua it is:

1 ~= 0

More info here:

Logically, not 1 becomes false. false == 0 becomes false. Typing in Lua and Luau is not like C.

Screenshot 2022-12-12 at 19.00.53

The only false truth values in Lua is nil and false as far as I know.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.