Hide an entity when player is dead?

I have hats on my players on my server and I am wondering if I can make the invisible when they die because they tweak out when the player dies. I am not sure how to get a players entity thats tied to them and hide it.

http://wiki.garrysmod.com/?title=Entity.SetColor Should make it invisible to everyone if you set it to 255,255,255,0 server-side

I got it working but seem to have a problem with the drawing. It seesm to go invisible randomly and you have to move far away for it to redraw or even rejoin for it to show. The only part of my entity that includes color is this. It is really anoyying and I am desperate to fix it.

[lua]
function ENT:Think()
if not self.Entity:GetOwner():Alive() then
self.Entity:SetColor(0,0,0,0)
return
end

if LocalPlayer() == self.Entity:GetOwner() then
	self.Entity:SetColor(0,0,0,0)
else
	self.Entity:SetColor(255,255,255,255)
end

end
[/lua]

Use the hook http://wiki.garrysmod.com/?title=Gamemode.PlayerDeath to set it invisible and use http://wiki.garrysmod.com/?title=Gamemode.PlayerSpawn to make it visible again.

basically that is what the code I posted above does.

Except that code calls unessecary functions every frame that waste resources and lag the game.

Tried using the hooks but it doesn’t seem to work.

The real one also checks if localplyer is owner and if so hides it on spawn, I only have one person so I need it to show me owner also.

The problem is that it takes like 5 deaths for it to go invis and then just never comes back. Also the don’t go invis when your dead only after you respawn.
[lua]
function HideHats()
self.Entity:SetColor( 0, 0, 0, 0 )
end
hook.Add( “PlayerDeath”, “HideHats”, HideHats )

function ShowHats()
self.Entity:SetColor( 255, 255, 255, 255 )
end
hook.Add( “PlayerSpawn”, “ShowHats”, ShowHats )
[/lua]

You didn’t specify a player. That will make it hide every hat once someone dies or show it when they respawn.

How do I specify a players specific ent?

[lua]
function ShowHats(ply)
if ent:GetNWString(“Owner”) == ply:GetName() then
ent:SetColor( 255, 255, 255, 255 )
end
end
hook.Add( “PlayerSpawn”, “ShowHats”, ShowHats )
[/lua]
Assuming you use SPP, of course.