Ammo HUD display error

I am working on getting a hud displayed and the code below works; however, when I die it causes an error and the hud disappears until I reset the gamemode. It says that client:GetActiveWeapon():Clip1() is a nil value and the hook couldn’t run.

To me it sounds like when I die it is trying to set mag_left equal to my ActiveWeapon():Clip1(), but since I am dead I don’t have a Clip1(). What confuses me is that if I am dead the function should end at my if !client:Alive() then return end and it shouldn’t run the mag_left = Clip1() code.

Anyone have any suggestions or tips as to why this may be causing an error?


 

function player_hud()

     if !client:Alive() then return end

     local mag_left = client:GetActiveWeapon():Clip1()

end



Also, is there a way to get a clip size of a weapon? I am working on getting a bar that decreases as you shoot, but I need the max amount of ammo in the clip size. I looked it up and I can find the current ammo and secondary, but i didn’t find anything that said clip size.

I wanted to stay away from creating an if statement for each weapon and defining the clipsize manually, however if that is the only way to do it then I will do it that way. I am open for some suggestions on that I guess.

Thanks.

[lua]
function player_hud()
if client:Alive() then
local mag_left = client:GetActiveWeapon():Clip1()
end
end
[/lua]
it was still running even while your dead

[lua]function player_hud()
if client:GetActiveWeapon() then
local mag_left = client:GetActiveWeapon():Clip1()
end
end[/lua]

I don’t know if that is the problem though.

I tried that code before but it shouldn’t matter since I have


if !client:Alive() then return end


in there. if the player isn’t alive, then return and end the function.

That gets run before any of the code does, so basically all you did is change from checking if they are dead to checking if they are alive. Right?

[editline]02:43AM[/editline]

Yeah, that makes sense, I don’t know why I didn’t think of that. Must be late for me or something.

I threw in that if client:GetActiveWeapon() and it still says Clip1 is a nil value after I die.

Here’s what I used:
[lua] – Ammunition
if LocalPlayer():GetActiveWeapon() and show_ammo == 1 then
if !LocalPlayer():GetActiveWeapon() or !LocalPlayer():GetActiveWeapon().Primary or !LocalPlayer():Alive() then return end
local ammo = LocalPlayer():GetActiveWeapon():Clip1() or 1
end[/lua]

What I had to do to fix it was this,

[lua]
if client():GetActiveWeapon() && client():GetActiveWeapon():IsWeapon() then
stuff in here
end

[/lua]

I can probably remove the client():GetActiveWeapon() and just leave the IsWeapon.