On a Players death run this function.

Hey guys… As some of you who know me quite a bit I am working on my servers JailBreak gamemode quite a lot and some of the things I need to do just… I would not have a clue where to start. Now I have been wanting to make things run on a certain player dying like the warden for example. The only problem is, is I don’t actually know a good and fully working way to get and run things on a players death. The only way I can manage deaths at the moment is if it was to be if not ply:Alive() and stuff… So here is a basic thing I would like to do.

On the wardens death I would like a


surface.PlaySound("trizone/jailbreak/deadwarden.wav")

and



chat.AddText(
Color(255,0,0), "WARDEN IS DEAD! PRISONERS GET FREEDAY!")
chat.PlaySound()


But there is no way obvious to me of making these run when the JB.wardenPlayer dies. All the warden setup is working and is setup here:



function JB:SetWarden(p)
	JB.wardenPlayer = p;
	for k,v in pairs(team.GetPlayers(TEAM_GUARD)) do
		v:SetModel("models/player/police.mdl")
	end
	p:SetModel("models/player/combine_soldier_prisonguard.mdl")
	umsg.Start("JSWP"); umsg.Entity(p); umsg.End();
end


So it can’t be anything to do with the warden not being identified… And also tried using



function JB:PlayerDeath( p, a, d )


BUT, it stops people from dying properly and conflicts with this script:



function JB:PlayerDeath( p, a, d )
	if p:Team() == TEAM_PRISONER then
		p:SetTeam(TEAM_PRISONER_DEAD);
		local e = ents.Create("jb_hands");
		e:SetModel(p:GetModel());
		e:SetPos(p:GetPos());
		e:GetAngles(p:GetAngles());
		e:SetVelocity(p:GetVelocity());
		e:Spawn();
		e:Activate();
		p:EmitSound(table.Random(DeathSounds),300,100);
		if p:Alive() then
			p:KillSilent()
			p:Spawn();
		end
	elseif p:Team() == TEAM_GUARD then -- or we simply check it.
		p:SetTeam(TEAM_GUARD_DEAD);
		local e = ents.Create("jb_hands");
		e:SetModel(p:GetModel());
		e:SetPos(p:GetPos());
		e:GetAngles(p:GetAngles());
		e:SetVelocity(p:GetVelocity());
		e:Spawn();
		e:Activate();
		p:EmitSound(table.Random(DeathSounds),300,100);
		if p:Alive() then
			p:KillSilent()
			p:Spawn();
		end
	end
	if JB:RoundStatus() ~= ROUND_END then
		JB:CheckRoundEnd();
	end
end


Its put me in a really hard position and now I just don’t know what to do… And please don’t ask me why it is spawning jb_hands on death T_T… It stop ragdolls spawning as dead bodies so it doesn’t dupe the ragdolls as I have a ragdolldeath fix addon…

I’m hoping this is going to help with a lot of other things also. Thanks for the help guys!

If you want to add more than one function to a hook, then you have to use hook.Add()

I’m horrible with hooks… I’ll have a play around thanks… If anyone has any other idea I would be really greatful.

[lua]
function yourFunction( ply, infli, killer )
print(ply:Nick() … " died!")
print(killer:Nick() … " killed " … ply:Nick())
end
hook.Add(“PlayerDeath”, “Random hook name”, yourFunction )

[/lua]

Will print the player who died and then who died and by who.

I think i have already tried playing with this hook but I will give it another go…

[editline]30th October 2013[/editline]

Tried this and it just fucked over the gamemode… :C



function yourFunction( ply, infli, killer )
	if JB.wardenPlayer == ply then
    PrintMessage( HUD_PRINTCENTER, "WARDEN IS DEAD! PRISONERS GET FREEDAY!" )
	chat.AddText(
	Color(255,0,0), "WARDEN IS DEAD! PRISONERS GET FREEDAY!")
	chat.PlaySound()
end
hook.Add("PlayerDeath", "Random hook name", yourFunction )


I’m not listing your faults to be a dick, just so you learn if you care to.

-You are missing an ‘end’
-Your last end is missing a ‘)’
-Missing a unique hook name, not sure if spaces are acceptable.
-If you are trying to print this on the screen for players on the warden team id use
‘if ply:Team() == TEAM_GUARD then’



function JB_DeathNotify( ply, infli, killer )
	if ply:Team() == TEAM_GUARD then
		PrintMessage( HUD_PRINTCENTER, "WARDEN IS DEAD! PRISONERS GET FREEDAY!" )
		chat.AddText(Color(255,0,0), "WARDEN IS DEAD! PRISONERS GET FREEDAY!")
		chat.PlaySound()
	end
end)
hook.Add("PlayerDeath", "JB_DeathNotifacation", JB_DeathNotify )


Also look at the formatting of this code now, makes it easier to read.

You’re using a combination of client and server functions/hooks.
PlayerDeath is a serverside hook. chat.AddText is clientside.

Good call, I’m not smart enough to spot things like that w/o testing it.

Here SatoshiAaron look how we can send clientside LUA from serverside files:



function JB_DeathNotify( ply, infli, killer )
	if ply:Team() == TEAM_GUARD then
		PrintMessage( HUD_PRINTCENTER, "WARDEN IS DEAD! PRISONERS GET FREEDAY!" )
		ply:SendLua( [[chat.AddText(Color(255, 0, 0, 255), "WARDEN IS DEAD! PRISONERS GET FREEDAY!" ) ]] )
		chat.PlaySound()
	end
end)
hook.Add("PlayerDeath", "JB_DeathNotifacation", JB_DeathNotify )


ms333 did I do that right? I did it in the browser, so no promises.

The close-bracket after the end of the function isn’t neccesary. (It’s not valid syntax)

chat.PlaySound is clientside too.
Localize the function as well.

I’m an idiot, I knew that. I don’t know the terminology for it, but I forgot this was coded as a normal function. In my head I was thinking this:



hook.Add("PlayerSpawn", "PlayerSpawnShit", function()
print("Hello")
end)


What a stupid stoner.

Thats not what I’m trying to do… I now understand that chat.AddText is client now you have told me but I want it to trigger when the warden dies… Not any random member of guard team

Then change the if statement to what you want it to check…