Error with Ammo HUD

Currently trying to make the “Clip1” part of my gamemode’s HUD. Here’s my code:


	if (ply:GetActiveWeapon():Clip1() != -1) then
		draw.SimpleText(LocalPlayer():GetActiveWeapon():Clip1(), "DermaLarge", 1300 + 30,ScrH() - 63, Color(255,255,255), TEXT_ALIGN_RIGHT, TEXT_ALIGN_TOP )
	else
		return false
	end

Here’s my error (Occurs upon death):


[ERROR] gamemodes/rogue/gamemode/cl_init.lua:78: attempt to call method 'Clip1' (a nil value)
  1. v - gamemodes/rogue/gamemode/cl_init.lua:78
   2. unknown - lua/includes/modules/hook.lua:84


Any help is appreciated.

Are you sure that function is running clientside and not serverside as well?

All the code here is clientside. Nothing about ‘Clip1’ on serverside.

Wait, I just noticed, you seem to use ‘ply’ when checking the clip isn’t -1, but then you use LocalPlayer() later. Have you defined ply?

“ply” has been defined. I just tried changing it to:


	if (LocalPlayer():GetActiveWeapon():Clip1() != -1) then
		draw.SimpleText(LocalPlayer():GetActiveWeapon():Clip1(), "DermaLarge", 1300 + 30,ScrH() - 63, Color(255,255,255), TEXT_ALIGN_RIGHT, TEXT_ALIGN_TOP )
	else
		return false
	end

But the error occurs still.

[editline]28th August 2016[/editline]

I managed to fix it by doing this:



	if LocalPlayer():GetActiveWeapon():IsValid() then
		draw.SimpleText(LocalPlayer():GetActiveWeapon():Clip1(), "DermaLarge", 1300 + 30,ScrH() - 63, Color(255,255,255), TEXT_ALIGN_RIGHT, TEXT_ALIGN_TOP )
	else
		return false
	end