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.
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.
[url]http://wiki.garrysmod.com/?title=Player.Frags[/url]
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:
[code]
Kills: <Current amount> out of <Amount needed for next gun>
Next weapon: <Next weapon>
[/code]
Great got everything to work! Thanks everyone :D
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() > 25 then
NextWeapon = "Glock 30"
elseif pl:Frags() > 50 then
NextWeapon = "Berretta M-92"
elseif pl:Frags() > 75 then
NextWeapon = "Desert Eagle"
elseif pl:Frags() > 100 then
NextWeapon = "TMP"
elseif pl:Frags() > 125 then
NextWeapon = "MP-5K"
elseif pl:Frags() > 150 then
NextWeapon = "AKS-74u"
elseif pl:Frags() > 175 then
NextWeapon = "FN-P90"
elseif pl:Frags() > 200 then
NextWeapon = "SPAS-12"
elseif pl:Frags() > 225 then
NextWeapon = "Winchester"
elseif pl:Frags() > 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
[code]ERROR: GAMEMODE:'HUDPaint' Failed: ZombieHorde/gamemode/cl_init.lua:32: attempt to concatenate global 'NextWeapon' (a nil value)[/code]
Also, the "Kills" work fine, but is there someway to make it say:
[code]<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"[/code]
Well
[lua]function GM:HUDPaint()
local pl = client or LocalPlayer()
if(!pl:Alive()) then return end
NextWeapon = "Glock 30"
Needed = 25
if pl:Frags() > 25 then
NextWeapon = "Beretta M-92"
Needed = 50
elseif pl:Frags() > 50 then
NextWeapon = "Desert Eagle"
Needed = 75
elseif pl:Frags() > 75 then
NextWeapon = "TMP"
Needed = 100
elseif pl:Frags() > 100 then
NextWeapon = "MP-5K"
Needed = 125
elseif pl:Frags() > 125 then
NextWeapon = "AKS-74u"
Needed = 150
elseif pl:Frags() > 150 then
NextWeapon = "FN-P90"
Needed = 175
elseif pl:Frags() > 175 then
NextWeapon = "SPAS-12"
Needed = 200
elseif pl:Frags() > 200 then
NextWeapon = "Winchester"
Needed = 225
elseif pl:Frags() > 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...
Great thanks! Works perfect, thanks for the help and your patience!
Sorry, you need to Log In to post a reply to this thread.