Custom Gamemode Error - attempt to call method 'GetActiveWeapon' (a nil value)

Hello, I am currently making a custom gamemode to practice my lua skills. I’ve encountered an error when creating the HUD for my ammo. Could I have some help please?
Here is the error:


[ERROR] gamemodes/deathmatch/gamemode/cl_hud.lua:93: attempt to call method 'GetActiveWeapon' (a nil value)
  1. unknown - gamemodes/deathmatch/gamemode/cl_hud.lua:93
   2. include - [C]:-1
    3. unknown - gamemodes/deathmatch/gamemode/cl_init.lua:2


Here is the relevant code:


if(client:GetActiveWeapon():IsValid()) then
	if (client:GetActiveWeapon():GetPrintName() != nil) then
		draw.SimpleText(client:GetActiveWeapon():GetPrintName(),"Trebuchet24",260,ScrH() - 60,Color(255,255,255), 0, 0)
	end

	if (client:GetActiveWeapon():Clip1() != -1) then
		draw.SimpleText(client:GetActiveWeapon():Clip1().."/"..client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()),"Ammo", 260, ScrH() - 40,Color(255,255,255), 0,0)
	else
		draw.SimpleText(client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType()),"Ammo",260,ScrH() - 40,Color(255,255,255),0,0)
	end
end

Just in case its important, client has been declared as LocalPlayer().

Any help would be appreciated. Thank you.

Use [lua]if(IsValid(client) and IsValid(client:GetActiveWeapon())) then[/lua]

IsValid does what it says, checks if it’s valid, but in what case wouldn’t the client be valid to the function?

Since client is set to LocalPlayer() then the answer is during the first few frames after the rendering system has been initialized but before the player object has been fully created.