What does an exclamation mark in Lua do?

logical-operators, lua, negation

Solution

As others have said, `!` normally has no function in Lua, and the code you posted would not normally be valid. However, it's quite trivial to extend Lua's parser to allow for custom syntax, and it's not unheard of for projects which embed Lua to add "more familiar" C-style syntax such as `!var` and `!=` in addition to `not var` and `~=`. One notable project which does this is Garry's Mod, and I'm sure there are others.

Of course, using custom syntax when the normal syntax is available (or customising it in the first place) is best avoided, if possible, to avoid exactly this sort of confusion.

Problem

Question is in the title, really. I saw someone use this earlier and I didn't know what the `!` was used for. ``` local lowestIndex = 0; local lowestValue = false; for k, v in ipairs(playerElement) do if !lowestValue or v.value < lowestValue then lowestIndex = k; lowestValue = v; end end ```

Original source