differentiate between Player and NPC kills

Hi, currently I’m using this code on my scoreboard


tabs[ 3 ] = { name = "Frags", 	size = 0.1,	liveUpdate = false,	fetch = function( ply ) return ply:Frags() end }

My goal is to differentiate between a player kill and an NPC kill so it feeds back to my scoreboard. I’m not sure how to go about this, I’m almost certain I need to create a function or meta function that can be called, maybe something along the lines of


function NPCDeath(npc, killer,weapon)
	if(true) then
		if(npc != killer) then
			if(killer:IsPlayer()) then
				killer:AddFrags(1)
			end
		end
	end

hook.Add( "OnNPCKilled", "fragset", NPCDeath )

This still doesn’t differentiate between the NPC and Player kills though. Any help would be appreciated, thanks in advance!

Found this bit of code


function NPCKillCounter( npc, killer, weapon )
	if not killer:IsPlayer() then return end

	if killer.KillCount then -- Check if the KillCount variable exists
		killer.KillCount = killer.KillCount + 1 -- If the variable exists then increase its value by 1
	else
		killer.KillCount = 1 -- If the variable doesn't exist then set it to 1
	end
end

hook.Add( "OnNPCKilled", "NPCKillCounter", NPCKillCounter ) -- Register the hook

Still having trouble implementing this though.

You can count how many NPCs a player has killed in a separate place. For example (untested):
[lua]hook.Add(“OnNPCKilled”, “CountKills”, function(npc, attacker)
if IsValid(attacker) and attacker:IsPlayer() then
attacker.NPCKills = (attacker.NPCKills or 0) + 1
end
end)[/lua]

Then ply.NPCKills will be the total number of NPCs that player has killed.

First of all, thanks for your quick reply!

Threw this in my autorun and configured that part of my scoreboard with this code


tabs[ 6 ] = { name = "NPC", 	size = 0.1,	liveUpdate = true, 	fetch = function( ply ) return ply:NPCKills() end }

I get a return with this


attempt to call method 'NPCKills' (a nil value)


I wonder if im placing the code wrong.

Wrong: ply:NPCKills()
Right: (ply.NPCKills or 0)

No errors this time around, no NPC kills are being recorded though. The counter sits at 0. I tried the code both in the config for the scoreboard, also in my autorun. also as a last ditch effort, tried it in a custom module.

OnNPCKilled is a serverside hook. You’re gonna have to use the net library to update kills to the players. (or networked vars)

Ah, good point, I hadn’t thought of that. I recommend you use

Entity:SetNWInt and

Entity:GetNWInt.

Also note, you can use SetNW2Int and GetNW2Int, which work the same as the above links explain. It’s hard to explain why but the NW2 variant is better.

[editline]27th November 2016[/editline]

[lua]hook.Add(“OnNPCKilled”, “CountKills”, function(npc, attacker)
if IsValid(attacker) and attacker:IsPlayer() then
attacker:SetNW2Int(“NPC Kills”, attacker:GetNW2Int(“NPC Kills”) + 1)
end
end)[/lua]