• Kill Counter for players.
    20 replies, posted
Hi guys I need to make a kill counter for when a player kills another player. 'not NPC' I have found a snippet of code and I was trying to modify it, but it isn't working. If someone could give me some guidance or show me how to do it I would be very grateful. This is what I have been trying to use.. function NPCKillCounter( ply, 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( "OnPlayerKilled", "PlayerKillCounter", PlayerKillCounter ) -- Register the hook function killcounter() draw.WordBox( 8, ScrW() - 920, ScrH() - 98, "KillCount: "..LocalPlayer():GetNWInt("killcounter"),"default",Color(200,0,0,0),Color(255,255,255,255)) end hook.Add("HUDPaint","KillCounter",killcounter) Thank you for any advice/help you can give! -Duby
Use [lua] tags then I'll help
So sorry I haven't used it before? Um tags? Do you mean comment ? Or? Never heard of 'Tag before' I am new here. :P
[QUOTE=DubyxD;42764968]So sorry I haven't used it before? Um tags? Do you mean comment ? Or? Never heard of 'Tag before' I am new here. :P[/QUOTE] [lua] code [/.lua] (without dot)
Isn't PlayerDeath serverside and HUDPaint clientside ?
Meta functions, look at the darkRP money system, same stuff.
Ok here. :) [lua] function NPCKillCounter( ply, 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( "OnPlayerKilled", "PlayerKillCounter", PlayerKillCounter ) -- Register the hook function killcounter() draw.WordBox( 8, ScrW() - 920, ScrH() - 98, "KillCount: "..LocalPlayer():GetNWInt("killcounter"),"default",Color(200,0,0,0),Color(255,255,255,255)) end hook.Add("HUDPaint","KillCounter",killcounter) [/lua]
-snip- Sorry dood, reading mobile is challenging :<
[QUOTE=Koolaidmini;42765131]Duby you literally posted a copy of his script.[/QUOTE] Duby created the thread, he just added lua tags. He doesn't know that he can edit the OP.
I was asked to change it to that format, I just need help. Also obviously I knew how to edit. there's a nice big button saying 'Edit' on the screen. Ty for any help though.
OnPlayerKilled isn't actually a hook, is it? Cant find it, anyway. Try using PlayerDeath or OnNPCKilled
sh_killcount.lua [code] local Player = FindMetaTable("Player") function Player:SetKills( amount ) self:SetNWInt( "Kills", amount ) self:SaveKills() end function Player:GetKills() return self:GetNWInt( "Kills" ) end function Player:SaveKills() local kills = self:GetKills() self:SetPData( "kills", kills ) end function Player:AddKill() local curKills = self:GetKills() self:SetMoney( curKills + 1 ) end [/code] sv_killcount.lua [code] function FirstSpawn( ply ) local kills = ply:GetPData("kills") if kills == nil then ply:SetPData("kills", 0 ) ply:SetKills( 0 ) else ply:SetKills( kills ) end end hook.Add( "PlayerInitialSpawn", "InitKillsOnSpawn", FirstSpawn ) function playerDies( victim, weapon, killer ) if victim:IsPlayer() then killer:AddKill() end End hook.Add( "PlayerDeath", "playerDeathTest", playerDies ) [/code] I'm on mobile, don't freak if there's an error.
I see, thank you very much for this. I will give it a go! :) Also if you would like to help me on another topic. I have tried everything and it isn't working, so any knowledge would be more than welcome. :) 'sorry if this is off topic'. [url]http://facepunch.com/showthread.php?t=1321628&p=42764417#post42764417[/url]
[QUOTE=WhitePilot;42765047]Isn't PlayerDeath serverside and HUDPaint clientside ?[/QUOTE] It is, but I didn't even bother reading the code without it being properly highlighted. And yeah, as WhitePilot said, it won't work without networking. [QUOTE=Koolaidmini;42765564]sh_killcount.lua [code] local Player = FindMetaTable("Player") function Player:SetKills( amount ) self:SetNWInt( "Kills", amount ) self:SaveKills() end function Player:GetKills() return self:GetNWInt( "Kills" ) end function Player:SaveKills() local kills = self:GetKills() self:SetPData( "kills", kills ) end function Player:AddKill() local curKills = self:GetKills() self:SetMoney( curKills + 1 ) end [/code] sv_killcount.lua [code] function FirstSpawn( ply ) local kills = ply:GetPData("kills") if kills == nil then ply:SetPData("kills", 0 ) ply:SetKills( 0 ) else ply:SetKills( kills ) end end hook.Add( "PlayerInitialSpawn", "InitKillsOnSpawn", FirstSpawn ) [/code] I'm on mobile, don't freak if there's an error.[/QUOTE] Really ? NWInts, PData and 4 separate functions only for a silly kill counter ? If you want something more efficient then here: Clientside: [lua]local function drawKillCounter() --Your code here, but instead of LP():NWInt() use LP.playerKills end local function receiveKillCount() LocalPlayer().playerKills = net.ReadInt( 8 ) end net.Receive( 'NET_killCountUpdate', receiveKillCount )[/lua] Serverside: [lua]util.AddNetworkString( 'NET_killCountUpdate' ) local function updateKillCount( person, killer, weapon ) if not killer:IsPlayer() then return end killer.playerKills = killer.playerKills or 0 killer.playerKills = killer.playerKills + 1 net.Start( 'NET_killCountUpdate' ) net.WriteInt( killer.playerKills, 8 ) net.Send( killer ) end hook.Add( 'OnPlayerKill' )[/lua] Now you can draw, print, use, modify it anywhere just by using Player.playerKills serverside or LocalPlayer().playerKills clientside.
Ok so will the code above my last comment work? I haven't had time to try it yet.
I just updated what I posted, should work, just use LocalPlayer:GetKills() in your HUD to return total kills. @Neth, it was just for simplicity so he understands it, that's all, though I do like your method
[QUOTE=Koolaidmini;42766157]I just updated what I posted, should work, just use LocalPlayer:GetKills() in your HUD to return total kills. @Neth, it was just for simplicity so he understands it, that's all, though I do like your method[/QUOTE] But why teach him such a bad habit.. What is the point in sending the same info each time, even if the info hasn't changed. Not to mention 4 functions
[QUOTE=Netheous;42766202]But why teach him such a bad habit.. What is the point in sending the same info each time, even if the info hasn't changed. Not to mention 4 functions for everything.[/QUOTE] Again, your method is superior, I don't deny that, I'm just showing that for an example, don't take it the wrong way. I didn't really go al out, adjusting what he wrote earlier;
[QUOTE=Koolaidmini;42766226]Again, your method is superior, I don't deny that, I'm just showing that for an example, don't take it the wrong way. I didn't really go al out, adjusting what he wrote earlier;[/QUOTE] In that case, my apologies.
[QUOTE=Netheous;42766237]In that case, my apologies.[/QUOTE] Thanks for that method though, could come in handy in the future!
I am very grateful Thank you all for helping me ! ^^
Sorry, you need to Log In to post a reply to this thread.