Null entity problem

Im create hud script, he is working but when player dead, he is create error:
[ulib] gamemodes/abc/gamemode/cl_init.lua:111: Tried to use a NULL entity!

  1. GetClass - [C]:-1
  2. fn - gamemodes/abc/gamemode/cl_init.lua:111
    3. unknown - addons/ulib/lua/ulib/shared/hook.lua:109 (x8)

I dont know how to fix this, can u plese help me?
Its the part of this script what creating error:

hook.Add("HUDPaint", "hudmy", function()

	if (LocalPlayer():GetActiveWeapon():GetClass() == "weapon_hpwr_stick") then

		y = ScrH() * 0.859
		y2 = ScrH()/2 * 1.695
		y3 = ScrH()/2 * 1.695
		y4 = ScrH()/2 * 1.695
		y5 = ScrH()/2 * 1.695

	else

		y = ScrH() * 0.979
		y2 = ScrH()/2 * 1.935
		y3 = ScrH()/2 * 1.935
		y4 = ScrH()/2 * 1.935
		y5 = ScrH()/2 * 1.935

	end

LocalPlayer and PLAYER:GetActiveWeapon may return NULL if the player is invalid or if the player is simply not holding any weapons, so you need to control that.

local ply = LocalPlayer()
if ( not ply:IsValid() ) then return end

local weapon = ply:GetActiveWeapon()
if ( not weapon:IsValid() ) then return end

if ( weapon:GetClass() == "weapon_hpwr_stick" ) then

-- your code ...

Also, I seriously advise you to put in local variables your calculations outside the hook to improve the efficiency of your function.

1 Like

thank you