Confusion on the If Statement in Lua

So I have the following function in my NPC:

function ENT:AcceptInput(Name, Activator, Caller)

if !Activator.cantUse and Activator:IsPlayer() then

	Activator.cantUse = true



	timer.Simple(1.5, function()

		Activator.cantUse = false



– Source: (I’m creating my own NPC based off of his. If you do not believe that I purchased this script, I’ll be more than happy to show you.)

I don’t understand the “if !Activator.cantUse” part. I don’t know what the exclamation mark does? Please help! Thanks guys!

if !Activator.cantUse then
-- is the same as
if not Activator.cantUse then

It’s good practice to use the 2nd one, as the first one is garry

All the exclamation mark means is that it’s checking the opposite of the variable, so

local var = true

print( !var )

Would return ‘false’ since that’s the opposite.

Also, as said above, you can also just use ‘not’ in place of the exclamation mark

Also, the reason that code is checking that thing with the exclamation mark is just because it’s trying to find whether the player CAN use the entity, rather than if they can’t

You can also use the ! to inverse a collection of logical comparisons.

if !(cat && dog) then

that comparison is only false (does not execute the if statement) if both cat and dog are true
if cat = true, dog = false, statement is true
if dog = true, cat = false, statement is true
if dog = false, cat = false, statement is true
if dog = true, cat = true, statement is false

Please never use that again

i don’t actually write for gmod anymore, just a C++ habit

though gmod supports those operators thanks to garry so

So it doesn’t work with normal Lua which makes porting code with those symbols in it painful

just be glad it isn’t brainfuck

-,+[                         Read first character and start outer character reading loop
    -[                       Skip forward if character is 0
        >>++++[>++++++++<-]  Set up divisor (32) for division loop
                               (MEMORY LAYOUT: dividend copy remainder divisor quotient zero zero)
        <+<-[                Set up dividend (x minus 1) and enter division loop
            >+>+>-[>>>]      Increase copy and remainder / reduce divisor / Normal case: skip forward
            <[[>+<-]>>+>]    Special case: move remainder back to divisor and increase quotient
            <<<<<-           Decrement dividend
        ]                    End division loop
    ]>>>[-]+                 End skip loop; zero former divisor and reuse space for a flag
    >--[-[<->+++[-]]]<[         Zero that flag unless quotient was 2 or 3; zero quotient; check flag
        ++++++++++++<[       If flag then set up divisor (13) for second division loop
                               (MEMORY LAYOUT: zero copy dividend divisor remainder quotient zero zero)
            >-[>+>>]         Reduce divisor; Normal case: increase remainder
            >[+[<+>-]>+>>]   Special case: increase remainder / move it back to divisor / increase quotient
            <<<<<-           Decrease dividend
        ]                    End division loop
        >>[<+>-]             Add remainder back to divisor to get a useful 13
        >[                   Skip forward if quotient was 0
            -[               Decrement quotient and skip forward if quotient was 1
                -<<[-]>>     Zero quotient and divisor if quotient was 2
            ]<<[<<->>-]>>    Zero divisor and subtract 13 from copy if quotient was 1
        ]<<[<<+>>-]          Zero divisor and add 13 to copy if quotient was 0
    ]                        End outer skip loop (jump to here if ((character minus 1)/32) was not 2 or 3)
    <[-]                     Clear remainder from first division if second division was skipped
    <.[-]                    Output ROT13ed character from copy and clear it
    <-,+                     Read next character
]                            End character reading loop

Oh my god. Not only is it hard as fuck… But it’s also ugly as hell.

So, basically “if !Activator.cantUse” means if the activator CAN use?

Actually, I’d use the ! / != because most programming languages don’t use not.

Really it’s just preference, just now it’s gonna be more confusing if you try to switch to normal Lua. Lua 5.3 and onward especially because it introduced bitwise operators in the form of ! and &

Ehhhh, I don’t know about that.

It’s really up to you. Do you want to stay and only code Lua? If so then it’d probably be a better practice to use not / and / blah blah blah. But if youre say, wanting to learn like C++ use !=, &&, and !

You chastise him for using &&, yet you think using ! is perfectly fine? Neither are default lua operators; both were added by garry.

I have only heard that using C++ operators in gmod is bad but I have never heard anyone explain why.

Guys, no one cares about the difference between C++ operators/GLua’s/Lua’s

I wanted to know what it meant, not the difference between those operators. I don’t care which one should or shouldn’t be used. If they work, they work.

// Solved