HUD Random Lua Error when dying.


My gamemode has trouble with a lua error from my HUD that pops up everytime the player dies.

The Error:
[ERROR] gamemodes/darkrp/gamemode/client/hud.lua:137: attempt to call method ‘Clip1’ (a nil value)

  1. DrawCustomHUD2 - gamemodes//gamemode/client/hud.lua:137
  2. DrawHUD - gamemodes/darkrp/gamemode/client/hud.lua:407
    3. unknown - gamemodes/darkrp/gamemode/client/hud.lua:532

if !LocalPlayer():Alive() then return end

local ply = LocalPlayer()
local mag_left = LocalPlayer():GetActiveWeapon():Clip1()
local mag_extra = LocalPlayer():GetAmmoCount(ply:GetActiveWeapon():GetPrimaryAmmoType())

if IsValid (LocalPlayer():GetActiveWeapon()) and (LocalPlayer():GetActiveWeapon():GetClass() == "weapon_physgun" or LocalPlayer():GetActiveWeapon():GetClass() == "weapon_physcannon" or LocalPlayer():GetActiveWeapon():GetClass() == "gmod_tool" or LocalPlayer():GetActiveWeapon():GetClass() == "keys") and LocalPlayer():Alive() then return end


Check if the active weapon is valid before attempting to call methods on it. Something like this:
local ply = LocalPlayer()

if !ply:Alive() then return end

local active_weapon = ply:GetActiveWeapon()
local mag_left, mag_extra = 0, 0;

if( IsValid( active_weapon ) )then
	mag_left = active_weapon:Clip1()
	mag_extra = ply:GetAmmoCount(active_weapon:GetPrimaryAmmoType())
	if(active_weapon:GetClass() == "weapon_physgun" or active_weapon:GetClass() == "weapon_physcannon" or active_weapon:GetClass() == "gmod_tool" or active_weapon:GetClass() == "keys") and ply:Alive() then