Clip1 a nil value

Hi, I am making a gamemode, and I’m having a slight problem with the HUD.

When the player gets killed, the console gets spammed with script errors, due to not being able to find a weapon to get the clip information from.

[ERROR] gamemodes/elimination/gamemode/hud.lua:11: attempt to call method ‘Clip1’ (a nil value)

Here’s the code for the HUD. It’s very basic at the moment as I’m just using it as a place holder while I test a few things.



local HudsToHide = {"CHudHealth","CHudBattery","CHudAmmo"}
function HUDHide(EliminationHud)
if table.HasValue(HudsToHide, EliminationHud) then return false end 
end
hook.Add("HUDShouldDraw", "HudHide", HUDHide)

surface.CreateFont("CustomFont", {font="ScoreboardText", size=40, weight=250}) 

function GM:HUDPaint( ply )
local HP = LocalPlayer():Health()
local Ammo_Primary = LocalPlayer():GetActiveWeapon():Clip1()

surface.SetTextColor(255, 255, 255, 255) //RGB, Transparency
surface.SetTextPos(20, 1025)
surface.SetFont("CustomFont")
surface.DrawText("HP: ")
surface.DrawText(tostring(HP))

surface.SetTextColor(255, 255, 255, 255) //RGB, Transparency
surface.SetTextPos(1700, 1025)
surface.SetFont("CustomFont")
surface.DrawText("Cash: ")
surface.DrawText(tostring(LocalPlayer():GetMoney()))

surface.SetTextColor(255, 255, 255, 255) //RGB, Transparency
surface.SetTextPos(1700, 970) //Width x Height
surface.SetFont("CustomFont")
surface.DrawText("XP: 0/100")
//surface.DrawText(tostring(localplayer():GetXP()))

surface.SetTextColor(255, 255, 255, 255) //RGB, Transparency
surface.SetTextPos(20, 970) //Width x Height
surface.SetFont("CustomFont")
surface.DrawText("Ammo: ")
surface.DrawText(tostring(Ammo_Primary))
end


Any help is appreciated.

Do an IsValid check on LocalPlayer():GetActiveWeapon().

I fixed it now. I just realized I forgot to define when player dies and it switches to camera, to return false.

Here’s the code if anyone else has this issue.




local client = LocalPlayer()
if not client:Alive() then return end
if (client:GetActiveWeapon() == NULL or client:GetActiveWeapon() == "Camera") then return end