Can't call functions on arguments from hook callback

Hi, I am trying to add some fairly basic functionality to my server. However, I am having trouble using the arguments in a function that runs on PlayerDeath.

For example, the following code from here does not even work correctly:


function sayThatName(victim,inflictor,killer) //Use the same arguments as the original function you're hooking to
	victim:PrintMessage(HUD_PRINTTALK,killer:Nick().." killed you!
")
end
hook.Add("PlayerDeath","Inform Murder Victims",sayThatName)

Specifically, when a player dies and the function is called, the following error occurs twice:


[ERROR] lua/autorun/server/test.lua:2: attempt to call method 'Nick' (a nil value)
  1. fn - lua/autorun/server/test.lua:2
   2. unknown - addons/ulib/lua/ulib/shared/hook.lua:183
    3. TakeDamageInfo - [C]:-1
     4. unknown - gamemodes/terrortown/gamemode/player.lua:1083


Any suggestions? Are the function arguments nil for some reason? (I’m unfamiliar with Lua)

Edit: sorry I was wrong

This code gives the same error:


function sayThatName(victim,inflictor,killer)
    if ( killer ~= nil ) then
        victim:PrintMessage(HUD_PRINTTALK,killer:Nick().." killed you!
")
    else
        victim:PrintMessage(HUD_PRINTTALK,victim:Nick().." killed you!
")
    end
end
hook.Add("PlayerDeath","Inform Murder Victims",sayThatName)


[ERROR] lua/autorun/server/test.lua:3: attempt to call method 'Nick' (a nil value)
  1. fn - lua/autorun/server/test.lua:3
   2. unknown - addons/ulib/lua/ulib/shared/hook.lua:183
    3. TakeDamageInfo - [C]:-1
     4. unknown - gamemodes/terrortown/gamemode/player.lua:1083

Edit: A completely different issue, but my server has a SEGMENTATION FAULT whenever I stop it with the “exit” command. Anyone know why that might happen?

Try:



function sayThatName(victim,inflictor,killer)
    if ( killer ~= nil and killer ~= victim and killer:IsPlayer() ) then
        victim:PrintMessage(HUD_PRINTTALK, killer:Nick().." killed you!
")
    else
        victim:PrintMessage(HUD_PRINTTALK, victim:Nick()..", you died!
")
    end
end
hook.Add("PlayerDeath","Inform Murder Victims",sayThatName)


Don’t check against nil, check IsValid(). When killed by the world it will be Null, not nil. Other than that the last bit of code by Defcon should work for you.

NULL:IsPlayer returns false, so it should work in this case.