killer kills a player to get xp

ok coders i am trying to setup a gamemode that you kill someone you get 10 xp



function PlayerKilled( victim, killer, weapon )

	if killer:IsPlayer() then
			killer:SetNWInt( "xp", killer:GetNWInt("xp") + 10 )
	end;

end;
hook.Call("PlayerDeath","PlayerKilled", PlayerKilled)


-bad reading-

You don’t need to check that the victim is a player because the hook wouldn’t be called unless that was already true.

@OP, use hook.Add instead.

so like this?



function PlayerKilled( victim, killer, weapon )

	if killer:IsPlayer() then
			killer:SetNWInt( "xp", killer:GetNWInt("xp") + 10 )
	end;

end;
hook.Add("PlayerDeath","PlayerKilled", PlayerKilled() )


You had it right the first time, you just had to change the “Call” to “Add”.

soo… this right



function PlayerKilled( victim, killer, weapon )

	if victim:IsPlayer() then
			killer:SetNWInt( "xp", killer:GetNWInt("xp") + 10 )
	end;

end;
hook.Call("PlayerDeath","PlayerKilled", PlayerKilled() )


That’s exactly what you posted the first time. Are you not testing any of this? Just change hook.Call to hook.Add.

ok WHAT??? i first change the if killer:IsPlayer to victim:IsPlayer

why don’t you first read the countless threads we have already replied to.
As well as replacing hood.Call with hook.Add, you must also change your 3rd argument to a function object, otherwise it will try and use the returned object as the function object (which would be invalid).

alright then make it



hook.Add("PlayerDeath","PlayerKilled", function()
if killer:IsPlayer() then
			killer:SetNWInt( "xp", killer:GetNWInt("xp") + 10 )
	end;
end)


You are not parsing any arguments from the hook. Killer is undefined.

so how would i put it defining killer, like this?



hook.Add("PlayerDeath","PlayerKilled", function(killer, victim, weapon)

if killer:IsPlayer() then
	killer:SetNWInt("xp", killer:GetNWInt("xp") + 10)
end


end)


because i just did that with my friend and it didnt give me a xp at all

[editline]12th October 2013[/editline]

lol i suicided and i got 10 xp WHATTT???

That code should work. Although I would add a check to if victim == killer.

how would i check like



if victim == killer:IsPlayer() then bluhhh


This has to be trolling.

It has to be.

how is this trolling?
im a bit bad at the lua coding

[editline]12th October 2013[/editline]

oh i see so like



hook.Add("PlayerDeath","PlayerKilled", function(killer, victim, weapon)

if victim:IsPlayer() then
        if killer:IsPlayer() then
	killer:SetNWInt("xp", killer:GetNWInt("xp") + 10)
        end;

end;
end)


Stop posting if you not even listen to people or can’t read a hook. Using PlayerDeath as hook will make the function run then you die so ofc you get 10 XP on death.

Try killing yourself in game. Granted the addition was needed, but not the one you were complaining about. before replying to this thread, try and solve the problem for yourself. There is a difference between learning, and being spoon fed (and even at that you are not making it easy).

Programming requires rational thought through logical examination of a problem. If you are gaining XP for killing yourself, then obviously, you (the player object) is the killer variable. But knowing you died also means that you are also the victim variable.
A way to avoid gaining XP for killing yourself is to actually check, if you killed yourself.

Oh lord, how have you gotten so turned around.

First of all the arguments are all mixed up. PlayerDeath uses victim, inflictor, attacker in that order. You can’t switch those around or each variable will represent the wrong data.

Second, you need to check that the victim is not the attacker by using victim != attacker.

See below:
[lua]
hook.Add(“PlayerDeath”,“PlayerKilled”, function(victim, inflictor, attacker)
if attacker:IsPlayer() && victim != attacker then
attacker:SetNWInt(“xp”, attacker:GetNWInt(“xp”) + 10)
end
end)
[/lua]