Ban players who disconnect with a certain weapon in their hands

I already tried:



function GAMEMODE:PlayerDisconnected( ply )
    ActiveWeapon = ply:GetActiveWeapon()
    if ( ActiveWeapon == guigui_handcuffed ) then
        RunConsoleCommand(ulx ban ply:Name 5 You get banned.)
        PrintMessage( HUD_PRINTTALK, ply:Name.. " get banned." )
    end
    PrintMessage( HUD_PRINTTALK, ply:Name.. " left the game." )
end


guigui_handcuffed is set in the hands of the player when hancuffed.

But this code does nothing, even the PrintMessage does not print message.
The code is in lua/autorun/server

You should be getting a syntax error in your console about not using strings. “guigui_handcuffed” should be a string, and “ulx ban ply:Name 5 You get banned.” should be a set of args for each space.


if IsValid(ActiveWeapon) then
    if ActiveWeapon:GetClass()=="guigui_handcuffed" then
        ...
    end
end

Also, use local variables.

Unless you’re developing your own gamemode, do not override the gamemode functions, use hooks. (Or at least til you know what you’re doing)
fixed code:



local function plydisco(ply)
    local ActiveWeapon = ply:GetActiveWeapon()
    if ( ActiveWeapon == "guigui_handcuffed" ) then
        RunConsoleCommand("ulx ban",ply:Name(),"You got punished for leaving while being cuffed","1d")
        PrintMessage( HUD_PRINTTALK, ply:Name().. " got fucking rekt." )
    end
    PrintMessage( HUD_PRINTTALK, ply:Name().. " left the game." )
end

hook.Add("PlayerDisconnected","PunishLeavers",plydisco)


You don’t need to use globals, don’t forget () after metafunctions, get a lua tutorial on strings and var types. Good attempt, but lots of beginners mistakes.

getactiveweaapon returns nil sometimes. you have to check if it is valid or not.

Didn’t know that, actually makes sense when player doesn’t have any weapon equipped (aka stripped, could be problem with NRL systems?)

For OP:



    if ( IsValid(ActiveWeapon) and ActiveWeapon == "guigui_handcuffed" ) then


It returns NULL, not nil. That means you can do just ActiveWeapon:IsValid()

[editline]2nd September 2017[/editline]

You still have to use GetClass.

I didnt really know the difference between NULL and nil. I guess if it is entity it will be NULL. Thanks anyway

It does not work, the message “left the game” appears, but even if I change guigui_handcuffed to weapon_physgun and hold my physgun, I do not get banned. :s:

maybe check our fixes?

I do not need to check if the player have a valid weapon (is there other fixes that I don’t see?)

[editline]2nd September 2017[/editline]

I think the problem is that I have a RP name because I try this in DarkRP gamemode

As code_gs mentioned, you need to call :GetClass() on the active weapon as well.
Do it by either:



local ActiveWeapon = ply:GetActiveWeapon():GetClass()
-- OR
local ActiveWeapon = ply:GetActiveWeapon()
ActiveWeapon = ActiveWeapon:GetClass()


And yes, you do need to check if the player has a valid weapon. Imagine if player has no weapon in hand - which can happen and is very possible. (Disconnect on death? NRL system?) If he has no weapon, then your code will error.

Also, I am 100% sure that RPName doesn’t affect anything here.

[editline]2nd September 2017[/editline]

Wait actually don’t use my fix, call the :GetClass() in the statement, otherwise the Valid check will fail.

So what do I need to do ? Because after adding :GetClass() nothing prints anymore.

[editline]2nd September 2017[/editline]

There is this error:



[ERROR] addons/darkrp_quozul/lua/autorun/server/sv_autoban.lua:6: Tried to use a NULL entity!
  1. GetClass - [C]:-1
   2. fn - addons/darkrp_quozul/lua/autorun/server/sv_autoban.lua:6
    3. unknown - addons/ulib-master/lua/ulib/shared/hook.lua:109


Can you post the code you have?



local function plydisco(ply)
    local ActiveWeapon = ply:GetActiveWeapon():GetClass()
    if ( ActiveWeapon == "weapon_physgun" ) then
        PrintMessage( HUD_PRINTTALK, ply:Name().. " got banned." )
    end
    PrintMessage( HUD_PRINTTALK, ply:Name().. " left the game." )
end

hook.Add("PlayerDeath","PunishLeavers",plydisco)


I changed the code to simplify the tests

you cut something out, because 6th line erroring doesn’t have any sense.
And this error happens because you haven’t listened to us, the ActiveWeapon was a NULL, which means you didn’t have any IsValid check.

We can’t help you, if you simply don’t listen to us. You may think you don’t need something, and cut it out, but again, you’re the one asking for help - and we are just helping you.

[editline]2nd September 2017[/editline]

I’m going to post full code here, hopefully it works. I am also going to add comments, so hopefully you learn something



local function plydisco(ply) --Here, we define, create a local function - Local functions exist only in the same file as it is created in
    local ActiveWeapon = ply:GetActiveWeapon() -- We are defining a new variable which contains player held weapon, note: the ActiveWeapon can be empty if the player doesn't have any weapon - again, we're making it local - no need to make it globally available.
    if ( IsValid(ActiveWeapon) and ActiveWeapon:GetClass() == "guigui_handcuffed" ) then --We are checking here, if the variable (with the weapon) is valid, and if the class of the weapon equals something
        RunConsoleCommand("ulx ban",ply:Name(),"You got punished for leaving while being cuffed","1d") --self explanatory, the wiki states that we need to pass each argument as new string
        PrintMessage( HUD_PRINTTALK, ply:Name().. " got fucking rekt." ) --You seem to get that, good for you
    end
    PrintMessage( HUD_PRINTTALK, ply:Name().. " left the game." )
end

hook.Add("PlayerDisconnected","PunishLeavers",plydisco) --We are "sticking" that function we just made using existing in game hook system - this makes it that whenever there's an event happening in the game, the original function will call those "hooks", allowing us to add on our own behavior to those events. First argument is the event name. You can see all of them on gmod wiki. Second argument is our unique name, in case we ever need to remove that hook. Third argument is our function callback, so whenever this event gets run, that function that we supplied will be run as well. Since we are referencing to the function (it is different than calling it or creating it) we don't add the ()'s around them. Hook arguments like Player get automatically passed over to our function.


I believe you need to separate every spaced command as an arg in RunConsoleCommand. “ulx” and “ban” should be separate, and so should every word.

honestly, phuck that, code will only become more and more gimmicky for already new user like OP

use (better imo, if " aren’t confusing for him)



game.ConsoleCommand( "ulx ban "..ply:Name().." \"You got punished for leaving the server while being cuffed\" 1d" )


And now, how I unban myself :smiley:
Thanks guys

I have a GAMEMODE:PlayerDeath in the script, I turn it into hook

Sorry if I don’t understand English as easely as you