• Gmod killcounter error
    8 replies, posted
I'm making a game-mode for Gmod, and I've only just started to program. I've been having a little trouble with my leveling up codes. It goes like this in the init file: function KillCounter( ply, Kills ) Kills = (Kills + 1) if Kills == 5 then ply:Give( "weapon_smg1" ) ply:SetMaxHealth( 125, true ) ply:PrintMessage( HUD_PRINTTALK, "(My message)" ) elseif Kills == 15 then ply:Give( "weapon_crossbow" ) ply:SetMaxHealth( 150, true ) ply:PrintMessage( HUD_PRINTTALK, "(My message)" ) elseif Kills == 25 then ply:Give( "weapon_ar2" ) ply:SetMaxHealth( 200, true ) ply:PrintMessage( HUD_PRINTTALK, "(My message)" ) else ply:PrintMessage( HUD_PRINTTALK, "Kills: "..Kills ) end end hook.Add("OnNPCKilled","KillCounter", KillCounter) When I start up Gmod with my mod, when i kill a NPC, an error pops up saying : Hook 'KillCounter' Failed: [gamemodes\soggie-mod\gamemode\init.lua:39] attempt to perform arithmetic on local 'Kills' (a userdata value) I'm a noob in a tight spot and scince i came up with the code myself, i'm wondering if anyone could help me fix it? Soggie
That's not how that hook works. [b][url=wiki.garrysmod.com/?title=Gamemode.OnNPCKilled]Gamemode.OnNPCKilled [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] gives three variables, the NPC that was killed, the entity that killed it, and the weapon used. To get a Kills variable, you would have to check if the entity is a player, and if it is, use a function to get his kills.
okay i'l try that thanks [editline]4th December 2010[/editline] So would this work? : function GM:OnNPCKilled( victim, killer, weapon ) if killer:GetClass() == LocalPlayer() then KillCounter() end end
Use [lua ] and [/lua ] (remove the space) and put your code in there, it looks nicer. what your doing is adding 1 to the entity that killed the npc, what you'l need to do is change your function to [lua] function KillCounter ( npc, killer, weapon )[/lua] and then use killer:IsPlayer() to make sure the killer is a player, after that do [lua]kills = kills + 1[/lua] and also be sure to do [lua] local kills = 0 [/lua] on the first line after the function Edit: @the above, no it wouldn't work, because LocalPlayer() is for clientside Edit2: also this variable "Kills" would be the same for all players, to set it on a per-player basis, replace "kills" with "killer.kills"
Okay just about to test it. [editline]4th December 2010[/editline] wait would i still have an hook?
[QUOTE=Soggie;26475647]Okay just about to test it. [editline]4th December 2010[/editline] wait would i still have an hook?[/QUOTE] Yes. another thing, replace "ply:" with "killer:", since ply isn't defined anywhere.
Read this. [url]http://wiki.garrysmod.com/?title=Kill_Counter[/url]
Thanks Zephilinox, thanks to you i finally have it working.
[QUOTE=Soggie;26475846]Thanks Zephilinox, thanks to you i finally have it working.[/QUOTE] No problem.
Sorry, you need to Log In to post a reply to this thread.