Counting Kills to Gain Weapons (NPCs)

I was wondering how to count the amount of kills a player has and when they reach a certain amount of kills they are given a weapon (similar to Zombie Survival).

Example:
5 Kills - Crossbow
10 Kills - Pistol
15 Kills - AR2
etc.

The kills would count when you kill an NPC.

**[Gamemode.OnNPCKilled

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.OnNPCKilled)**

Ninjaed :expressionless:

Something like this?

[lua]function GM:OnNPCKilled(attacker)
if attacker:IsPlayer() then
attacker:AddFrags(1)
end
end[/lua]

[editline]07:56PM[/editline]
I just don’t know how to go about giving the weapons on the desired amount of kills.

See my previous “Give” link, and the following link.

Thanks both of you, I got it done with this code.
[lua]function GM:OnNPCKilled(ent, inflictor, attacker)
if attacker:IsPlayer() then
attacker:AddFrags(1)
self:CheckScore(attacker)
end
end

function GM:CheckScore(pl)
local score = pl:Frags()
if score == 5 then
pl:Give(“weapon_stunstick”)
elseif score == 10 then
pl:Give(“weapon_pistol”)
elseif score == 15 then
pl:Give(“weapon_crossbow”)
end
end[/lua]
The weapons and kill amounts are just placeholders. I am not sure if this is the best way, however it did work.

[editline]04:55AM[/editline]
I was also wondering how I could maybe make a HUD element with the information…
Example:



Kills: <Current amount> out of <Amount needed for next gun>
Next weapon: <Next weapon>


[lua]
surface.DrawText(“Kills”…LocalPlayer():GetFrags())

if LocalPlayer():GetFrags() > 5 then
NextWeapon = “StunStick”
elseif LocalPlayer():GetFrags() > 10 then
NextWeapon = “Pistol”
elseif LocalPlayer():GetFrags > 15 then
NextWeapon = “Crossbow”
end

surface.DrawText("Next weapon: "…NextWeapon)
[/lua]

Frags is a shared accessor function I think. And of course you will need to use more then surface.DrawText, you would need to set the color and position. Easy to find they’re in the surface library.

Great got everything to work! Thanks everyone :smiley:

Actually, no wait… I put this code in
[lua]
function GM:HUDPaint()
surface.CreateFont(“Lucida Console”, 30, 400, true, false, “hudtxt”)
local pl = client or LocalPlayer()
if(!pl:Alive()) then return end

surface.SetTextColor(255, 255, 255, 255)
surface.SetTextPos(ScrW()/2.5+45, ScrH()-79)
surface.DrawText("Kills: "..pl:Frags())
if pl:Frags() &gt; 25 then
	NextWeapon = "Glock 30"
elseif pl:Frags() &gt; 50 then
	NextWeapon = "Berretta M-92"
elseif pl:Frags() &gt; 75 then
	NextWeapon = "Desert Eagle"
elseif pl:Frags() &gt; 100 then
	NextWeapon = "TMP"
elseif pl:Frags() &gt; 125 then
	NextWeapon = "MP-5K"
elseif pl:Frags() &gt; 150 then
	NextWeapon = "AKS-74u"
elseif pl:Frags() &gt; 175 then
	NextWeapon = "FN-P90"
elseif pl:Frags() &gt; 200 then
	NextWeapon = "SPAS-12"
elseif pl:Frags() &gt; 225 then
	NextWeapon = "Winchester"
elseif pl:Frags() &gt; 250 then
	NextWeapon = "AK-47"
end
surface.SetTextColor(255, 255, 255, 255)
surface.SetTextPos(ScrW() / 2.5 + 43, ScrH() - 54)
surface.DrawText("Next Weapon: "..NextWeapon)

end
[/lua]
And get the error


ERROR: GAMEMODE:'HUDPaint' Failed: ZombieHorde/gamemode/cl_init.lua:32: attempt to concatenate global 'NextWeapon' (a nil value)

Also, the “Kills” work fine, but is there someway to make it say:


<amount of kills> out of <kills needed>
So say a weapon takes 10 kills to get to the output would be:
"Kills: 5 out of 10"

You do NOT create a font in a function that gets called a lot. EVER.

[lua]
surface.CreateFont(“Lucida Console”, 30, 400, true, false, “hudtxt”)
function GM:HUDPaint()
local pl = client or LocalPlayer()
if(!pl:Alive()) then return end
NextWeapon = “USP”

surface.SetTextColor(255, 255, 255, 255)
surface.SetTextPos(ScrW()/2.5+45, ScrH()-79)
surface.DrawText("Kills: "..pl:Frags())
if pl:Frags() &gt; 25 then
	NextWeapon = "Glock 30"
elseif pl:Frags() &gt; 50 then
	NextWeapon = "Berretta M-92"
elseif pl:Frags() &gt; 75 then
	NextWeapon = "Desert Eagle"
elseif pl:Frags() &gt; 100 then
	NextWeapon = "TMP"
elseif pl:Frags() &gt; 125 then
	NextWeapon = "MP-5K"
elseif pl:Frags() &gt; 150 then
	NextWeapon = "AKS-74u"
elseif pl:Frags() &gt; 175 then
	NextWeapon = "FN-P90"
elseif pl:Frags() &gt; 200 then
	NextWeapon = "SPAS-12"
elseif pl:Frags() &gt; 225 then
	NextWeapon = "Winchester"
elseif pl:Frags() &gt; 250 then
	NextWeapon = "AK-47"
end
surface.SetTextColor(255, 255, 255, 255)
surface.SetTextPos(ScrW() / 2.5 + 43, ScrH() - 54)
surface.DrawText("Next Weapon: "..NextWeapon)

end
[/lua]

You don’t create fonts in functions anyway, unless it only gets called once.

[lua]
surface.CreateFont(“Lucida Console”, 30, 400, true, false, “hudtxt”)
function GM:HUDPaint()
local pl = client or LocalPlayer()
if(!pl:Alive()) then return end
NextWeapon = “USP”
Needed = 25

if pl:Frags() &gt; 25 then
	NextWeapon = "Glock 30"
	Needed = 50
elseif pl:Frags() &gt; 50 then
	NextWeapon = "Berretta M-92"
	Needed = 75
elseif pl:Frags() &gt; 75 then
	NextWeapon = "Desert Eagle"
	Needed = 100
elseif pl:Frags() &gt; 100 then
	NextWeapon = "TMP"
	Needed = 125
elseif pl:Frags() &gt; 125 then
	NextWeapon = "MP-5K"
	Needed = 150
elseif pl:Frags() &gt; 150 then
	NextWeapon = "AKS-74u"
	Needed = 175
elseif pl:Frags() &gt; 175 then
	NextWeapon = "FN-P90"
	Needed = 200
elseif pl:Frags() &gt; 200 then
	NextWeapon = "SPAS-12"
	Needed = 225
elseif pl:Frags() &gt; 225 then
	NextWeapon = "Winchester"
	Needed = 250
elseif pl:Frags() &gt; 250 then
	NextWeapon = "AK-47"
	Needed = 9001 -- Meme Shit
end

surface.SetTextColor(255, 255, 255, 255)
surface.SetTextPos(ScrW()/2.5+45, ScrH()-79)
surface.DrawText("Kills: "..pl:Frags().." out of "..Needed)

surface.SetTextColor(255, 255, 255, 255)
surface.SetTextPos(ScrW() / 2.5 + 43, ScrH() - 54)
surface.DrawText("Next Weapon: "..NextWeapon)

end
[/lua]

Well
[lua]function GM:HUDPaint()
local pl = client or LocalPlayer()
if(!pl:Alive()) then return end
NextWeapon = “Glock 30”
Needed = 25

if pl:Frags() &gt; 25 then  
	NextWeapon = "Beretta M-92"  
	Needed = 50  
elseif pl:Frags() &gt; 50 then  
	NextWeapon = "Desert Eagle"  
	Needed = 75  
elseif pl:Frags() &gt; 75 then  
	NextWeapon = "TMP"  
	Needed = 100  
elseif pl:Frags() &gt; 100 then  
	NextWeapon = "MP-5K"  
	Needed = 125  
elseif pl:Frags() &gt; 125 then  
	NextWeapon = "AKS-74u"  
	Needed = 150  
elseif pl:Frags() &gt; 150 then  
	NextWeapon = "FN-P90"  
	Needed = 175  
elseif pl:Frags() &gt; 175 then  
	NextWeapon = "SPAS-12"  
	Needed = 200  
elseif pl:Frags() &gt; 200 then  
	NextWeapon = "Winchester"  
	Needed = 225  
elseif pl:Frags() &gt; 225 then  
	NextWeapon = "AK-47"  
	Needed = 250
end  

surface.SetTextColor(255, 255, 255, 255)  
surface.SetTextPos(ScrW()/2.5+45, ScrH()-79)  
surface.DrawText("Kills: "..pl:Frags().." out of "..Needed)  

surface.SetTextColor(255, 255, 255, 255)  
surface.SetTextPos(ScrW() / 2.5 + 43, ScrH() - 54)  
surface.DrawText("Next Weapon: "..NextWeapon)  

end[/lua]
This is my code, but at 50 kills the “Needed amount” stays at 50, while the player kills still continue. So it would show something like “55 out of 50” Also the weapon stays at “Beretta M-92” There are no errors though…

-snip-

You have it using elseif. You need all those as seperate if statements or else it’ll only do the first one. The first 2:

[lua]
if pl:Frags() > 25 then
NextWeapon = “Beretta M-92”
Needed = 50
end
if pl:Frags() > 50 then
NextWeapon = “Desert Eagle”
Needed = 75
end
[/lua]

Great thanks! Works perfect, thanks for the help and your patience!