I was wondering if you can change nil so next time it has to show up on hud you can change it to say a number like say.
nil = 0
Thanks ~~
Post your code where your getting nil
if( CLIENT ) then
LocalPlayer().NPCKillCount = 0;
usermessage.Hook("KillCountIncreased", function( data )
hook.Call("PlayerKillCountChanged", GM, data:ReadChar(), data:ReadString(), data:ReadString());
end)
hook.Add("PlayerKillCountChanged", "ChangeNPCKillCount", function(kCount, victim, weapon)
LocalPlayer().NPCKillCount = kCount;
chat.AddText(Color(127, 127, 127), "You killed "..victim.."!");
end)
hook.Add("HUDPaint", "DrawKills", function()
if( LocalPlayer().NPCKillCount ) then
end
end)
end
if( SERVER ) then
hook.Add("OnNPCKilled", "KillCounter", function(victim, attacker, wep)
if ( !victim:IsValid() || !attacker:IsValid() || !attacker:IsPlayer() ) then return; end
if( attacker.NPCKillCount ) then
attacker.NPCKillCount = attacker.NPCKillCount + 1;
else
attacker.NPCKillCount = 1;
end
umsg.Start("KillCountIncreased", attacker);
umsg.Char( attacker.NPCKillCount );
umsg.String( tostring( victim:GetClass() ) );
umsg.String( tostring( wep:GetClass() ) );
umsg.End();
end)
end
Where is nil showing up?
I have hud for killing npcs, untill you kill one npc its nil.
What line is the error? Also, I'd move the PlayerKillCountChanged hook above the usermessage hook.
[QUOTE=Chessnut;33296421]What line is the error? Also, I'd move the PlayerKillCountChanged hook above the usermessage hook.[/QUOTE]
C-Unit, my friend made it. He is worse than me at lua to put things into perspective.
[QUOTE=Chessnut;33296421]What line is the error? Also, I'd move the PlayerKillCountChanged hook above the usermessage hook.[/QUOTE]
It makes no difference where it is..
[QUOTE=Blue Kirby;33306836]C-Unit, my friend made it. [b]He is worse than me at lua[/b] to put things into perspective.[/QUOTE]
Ha. Sure.
[QUOTE=Man Without Hat;33306975]It makes no difference where it is..[/QUOTE]
It is just to keep it away from any possible erroring, example:
[lua]
runHookFunction()
function runHookFunction
print "It would error.";
end;
[/lua]
It will never error, I coded it, I tested it. Especially with hooks, Adding a hook above where you call it doesn't matter at all. It's getting called and whatever is hooked into the event will be called as weel.
Have you ever thought of using the "or 0" thingy.
[lua]local value
print(value or 0)
value = 1
print(value or 0)
[/lua]
[code]0
1[/code]
Kind of hard to even read with the way it's formatted. And Kirby, pretty sure c-unit has done a lot more than you.
The problem is that you are trying to access the LocalPlayer entity before it exists, you need to run that in an InitPostEntity hook (I had to clean up your code, sorry):
[lua]if CLIENT then
hook.Add("InitPostEntity", "AddKillCountHooks", function()
LocalPlayer().NPCKillCount = 0
usermessage.Hook("KillCountIncreased", function(um)
hook.Call("PlayerKillCountChanged", GM, um:ReadChar(), um:ReadString(), um:ReadString())
end
)
hook.Add("PlayerKillCountChanged", "ChangeNPCKillCount", function(kCount, victim, weapon)
LocalPlayer().NPCKillCount = kCount
chat.AddText(Color(127, 127, 127), "You killed "..victim.."!")
end
)
hook.Add("HUDPaint", "DrawKills", function()
--HUD
end
)
end
)
else
hook.Add("OnNPCKilled", "KillCounter", function(victim, attacker, wep)
if not attacker:IsPlayer() then return end
if attacker.NPCKillCount then
attacker.NPCKillCount = attacker.NPCKillCount + 1
else
attacker.NPCKillCount = 1
end
umsg.Start("KillCountIncreased", attacker)
umsg.Char(attacker.NPCKillCount)
umsg.String(victim:GetClass())
umsg.String(wep:GetClass())
umsg.End()
end
)
end[/lua]
Also, unless you really need to work with this hook and field elsewhere, you can make your code more efficient:
[lua]if CLIENT then
hook.Add("InitPostEntity", "AddKillCountHooks", function()
local NPCKillCount = 0
usermessage.Hook("KillCountIncreased", function(um)
NPCKillCount = NPCKillCount + 1
chat.AddText(Color(127, 127, 127), "You killed "..um:ReadString().."!")
end
)
hook.Add("HUDPaint", "DrawKills", function()
--HUD
--Note: You would just use the variable NPCKillCount, not LocalPlayer().NPCKillCount
end
)
end
)
else
hook.Add("OnNPCKilled", "KillCounter", function(victim, attacker, wep)
if not attacker:IsPlayer() then return end
umsg.Start("KillCountIncreased", attacker)
umsg.String(victim:GetClass())
umsg.End()
end
)
end[/lua]
[QUOTE=TheDivinity;33314732]Kind of hard to even read with the way it's formatted. And Kirby, pretty sure c-unit has done a lot more than you.[/QUOTE]
I have, I'm also not 12.
[editline]17th November 2011[/editline]
Also, luamilkshake, the point of the hook wwas so that he could get the networ ked information anywhere, rather then just that file.
[QUOTE=Chessnut;33310506]It is just to keep it away from any possible erroring, example:
[lua]
runHookFunction()
function runHookFunction
print "It would error.";
end;
[/lua][/QUOTE]
That's redundant when it comes to events (Which hooks are) as by the time the event is first called after hooking, the function will have been created.
Sorry, you need to Log In to post a reply to this thread.