Cant call GetActiveWeapon():Clip1() when dead

Im trying to make bullets draw on my hud. It works during the life of my first spawn in a server, but after dying I get this error

Hook ‘hudAmmo’ Failed: addons\supahud\lua\autorun\client\bulletammo.lua:7: attempt to call method ‘Clip1’ (a nil value)

and then it never draws again. Any help please?

[lua]
bullet = surface.GetTextureID( “hud_bullet/ammobar” );

function hudAmmo()
if LocalPlayer():Alive() then

    local client = LocalPlayer();
    local mag_left = client:GetActiveWeapon():Clip1()
    if mag_left == 0 then
        return end
    howManyLoops = mag_left;
    for i=1,howManyLoops do
        surface.SetTexture( bullet );
        surface.SetDrawColor(255,255,225,255);
        surface.DrawTexturedRect(ScrW() - (64+(i*4)), 64, 32, 32);
    end
end    

end

hook.Add(“HUDPaint”, “hudAmmo”, hudAmmo);

[/lua]

Check if the weapon is valid.
[lua]
bullet = surface.GetTextureID( “hud_bullet/ammobar” );

function hudAmmo()
if LocalPlayer():Alive() then

    local client = LocalPlayer();  
    if client:GetActiveWeapon():IsValid() then -- Here :D
    local mag_left = client:GetActiveWeapon():Clip1()  
    if mag_left == 0 then  
        return end  
    howManyLoops = mag_left;  
    for i=1,howManyLoops do  
        surface.SetTexture( bullet );  
        surface.SetDrawColor(255,255,225,255);  
        surface.DrawTexturedRect(ScrW() - (64+(i*4)), 64, 32, 32);  
     end  
  end      

end
end

hook.Add(“HUDPaint”, “hudAmmo”, hudAmmo);
[/lua]

Tested and works.

Probably should add a check to make sure they’re holding a valid weapon.

[lua]bullet = surface.GetTextureID( “hud_bullet/ammobar” );

function hudAmmo()
if LocalPlayer():Alive() and !Player:GetActiveWeapon() == NULL and !Player:GetActiveWeapon() == “Camera” then

    local client = LocalPlayer();  
    local mag_left = client:GetActiveWeapon():Clip1()  
    if mag_left == 0 then  
        return end  
    howManyLoops = mag_left;  
    for i=1,howManyLoops do  
        surface.SetTexture( bullet );  
        surface.SetDrawColor(255,255,225,255);  
        surface.DrawTexturedRect(ScrW() - (64+(i*4)), 64, 32, 32);  
    end  
end      

end

hook.Add(“HUDPaint”, “hudAmmo”, hudAmmo); [/lua]

bwahahaha totally :ninja:'d

Works perfectly! Thank you!