AvatarImage on Hud

So I have a Avatar Image on my hud, but I doesn’t hide if a player is holding a camera. Now I have made it hide if you are holding the camera, but if you are killed, then it comes up with this error:



[ERROR] 
  1. unknown - [C]:-1

Error in hook HUDPaint: addons/darkrpmodification/lua/darkrp_modules/hud_zenekex_custom/cl_hud_v2.lua:190: Tried to use a NULL entity!
stack traceback:
	[C]: in function 'GetClass'
	addons/darkrpmodification/lua/darkrp_modules/hud_zenekex_custom/cl_hud_v2.lua:190: in function 'fn'
	addons/ulib/lua/ulib/shared/hook.lua:109: in function <addons/ulib/lua/ulib/shared/hook.lua:92>
	[C]: in function '•‎​‬‪•'

[ERROR] 
  1. unknown - [C]:-1


It Repeats multiple times. Now the line it is having a problem with is they one I use to hide the Avatar if the player is holding the camera. I have tried to make it check if the player is alive before it runs this code, and it works when I update it, but if a player rejoins my server, the error occurs again.

The Code Creating the error:


    if IsValid( LocalPlayer() ) && !IsValid( Avatar ) then
    
        Avatar= vgui.Create( "AvatarImage" )
    
		Avatar:SetSize( 110, 110 )
		Avatar:SetPos( 30,ScrH() -168 )
		Avatar:SetPlayer( LocalPlayer(), 128 )
		Avatar:ParentToHUD()
    	
    end

    if (LocalPlayer():GetActiveWeapon():GetClass() == "gmod_camera") then
    	Avatar:Hide()
    else
    	Avatar:Show()
    end


Any Ideas?

Since you can’t get the class of something NULL, you would need to check if


LocalPlayer():GetActiveWeapon()

is valid or not.

Wait, which line is 190?

Are you talking about this segment?



if (LocalPlayer():GetActiveWeapon():GetClass() == "gmod_camera") then
	Avatar:Hide()
else
	Avatar:Show()
end


Would this be a solution?



if Avatar and Avatar:IsValid() then
    if (LocalPlayer():GetActiveWeapon():GetClass() == "gmod_camera") then
	Avatar:Hide()
    else
	Avatar:Show()
    end
end


Alternatively, if you don’t have anything else in the draw function after this, you can just add



else
    return
end


in place f the “end” for your **first **if statement.

Unless I understood the problem wrong.

Thank you! I don’t know how I couldn’t figure that out, but that works. If anyone else is wondering, I used:



    if (LocalPlayer():GetActiveWeapon():IsValid()) then
    	if (LocalPlayer():GetActiveWeapon():GetClass() == "gmod_camera") then
   			Avatar:Hide()
    	else
    		Avatar:Show()
    	end
    end