Error with displaying ammo on HUD

Hi, im working on a gamemode and i got my hud to work but when a player dies it brings up this error message:


[ERROR] gamemodes/gtrp/gamemode/cl_init.lua:81: attempt to call method 'GetPrimaryAmmoType' (a nil value)
  1. fn - gamemodes/gtrp/gamemode/cl_init.lua:81
   2. unknown - addons/ulib/lua/ulib/shared/hook.lua:108


here is my code for the ammo hud


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

draw.DrawText( mag_left , "gtfont_ammo1", ScrW() - 110, ScrH() - 900, Color(255, 255, 255, 200), TEXT_ALIGN_CENTER)
draw.DrawText( mag_extra , "gtfont_ammo1", ScrW() - 50, ScrH() - 900, Color(200, 200, 200, 200), TEXT_ALIGN_CENTER)

if anyone can help i will be really thankful

Add in a validity check before drawing that information,

if( IsValid(LocalPlayer() ) && LocalPlayer():IsAlive() ) then

If you are dead then you don’t have any gun equipped, so getting the ammo will error.

no it didnt work and i think your code is also wrong :IsAlive() should Be :Alive()

[editline]4th May 2015[/editline]

as i said im not sure if it is wrong but gave me errors when i did it like that



if( IsValid(LocalPlayer() ) && LocalPlayer():Alive() ) then
	
  local mag_left = LocalPlayer():GetActiveWeapon():Clip1()
  local mag_extra = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType())

draw.DrawText( mag_left , "gtfont_ammo1", ScrW() - 110, ScrH() - 900, Color(255, 255, 255, 200), TEXT_ALIGN_CENTER)
draw.DrawText( mag_extra , "gtfont_ammo1", ScrW() - 50, ScrH() - 900, Color(200, 200, 200, 200), TEXT_ALIGN_CENTER)
	end


still brings same errors

Woops my bad, it should work.

If not then try

if !LocalPlayer():Alive() then return end
if(LocalPlayer():GetActiveWeapon() == NULL or LocalPlayer():GetActiveWeapon() == "Camera") then return end

thanks it works!