Question

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
Was it helpful?

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.

OTHER TIPS

It's a syntax error.

Some languages, mostly C and its relatives, use ! as a logical "not" operator, but Lua uses the not keyword instead, and does not use ! for anything as far as I know (not even as part of the inequality operator; it uses ~= where C uses !=).

You appear to have gotten hold of some Lua code written by someone who doesn't know that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top