Writing Hook to onplayerdeath

A Forum Thread for Garry's Mod

Hey everyone, I'm new to lua scripting (meaning I'm useless) I want to learn just a simple hook.
Okay, my friends and I were talking about hooking a player death to a caht bind, EG when I die in PH I saw in chat "Well that was an unexpected discovery" or when someone else dies it'll instantly say "[there name] couldn't handle the heat" instead of binding it to a key and pressing it when they die. I know this seems like a really amateur request, I've just been wanting to do this for a while now. Cheers

Posts

  • ficool2 avatar
    7,389 points Ranked 655th
    23 medals 4 legendary 4 rare
    • 2017 Top Contributor Medal icon
    • 30 submissions featured Medal icon
    • Reached 100 subscribers Medal icon
    • Returned 5000 times Medal icon
    • Returned 1000 times Medal icon
    • 10 submissions featured Medal icon
    29d
  • Reverend V92 avatar
    Reverend V92 username pic Joined 8y ago
    Away
    4,030 points Ranked 1263rd
    22 medals 3 rare
    • 2nd Place - A Bananite's Guide to... Contest Medal icon
    • 6 years a member Medal icon
    • Returned 1000 times Medal icon
    • Building Bridges Entrant Medal icon
    • Became a Studio Leader Medal icon
    • Reached 1,000 Points Medal icon
    28d
    There's several ways to do this. For a gamemode, this is the hook you're looking for.

    However, you're going to need access to the game mode itself to use that one, as it's a gamemode hook.

    If you just want to make a hook that runs on every player death, consider using the PlayerDeath hook.

    Rough untested example of this:
    if !ConVarExists( "vnt_death_messages" ) then CreateConVar(    "vnt_death_messages" , "0" , { FCVAR_REPLICATED , FCVAR_ARCHIVE } , "(Boolean) Toggles custom death messages when players die." ) end

    local function VNTPlayerDeathChatPrint( vic, atk, kil )
        if GetConVarNumber( "vnt_death_messages" ) != 0 then
            local PlayerList = player.GetAll( )
            local Victim = vic
            local Killer = kil
            if Victim:IsPlayer( ) then

                if Killer != nil then
                    
                    PlayerList:PrintMessage( HUD_PRINTCENTER , Victim .. " has been killed by " .. Killer )

                else

                    PlayerList:PrintMessage( HUD_PRINTCENTER , Victim .. " has been killed!" )

                end
                
            end
            
        end

    end
    hook.Add("PlayerDeath", "VNTPlayerDeathChatPrint", VNTPlayerDeathChatPrint)

    Source Engine Wizard avatar
    Mantra
    Source Engine Wizard

Embed

Share banner
Image URL:
HTML embed code:
BB embed code:
Markdown embed code:

Author

Ink Child avatar
Ink Child Joined 2y ago
Offline
199 points Ranked 19752nd

Ink Child
Sign up to access this!
Sign up to access this!
Sign up to access this!

Game

Sign up to access this!

Category

Share

  • Share on Reddit
  • Share on Twitter
  • Share on Facebook
  • Share on Google+

Stats

  • 59 Views
  • 2 Posts
  • 29d Submitted
bcp.crwdcntrl.net tracking pixel