The names remain after death

Hey
I got this code (Its show your nickname and the country flag under your head)
So I have some problems with this code that I dont know how to fix it
First I see the names even after the player died
And when the player invisible I can see the name too

Code


if(SERVER) then

	hook.Add("Initialize", "NamesOnHead", function()

		AddCSLuaFile();

		--[[
		local function addAllFiles(path) 
			local f,d = file.Find(path.."/*","GAME")
			for _,v in pairs(f) do
				resource.AddFile(path.."/"..v)
			end
			for _,v in pairs(d) do
				addAllFiles(path.."/"..v)
			end
		end
		
		addAllFiles("materials/countryicons16") 

		resource.AddFile("resource/fonts/bankgthd.ttf");
		]]--

		util.AddNetworkString("sendCountry");

		net.Receive("sendCountry", function(_, cl)
			cl:SetNWString("country", net.ReadString() or "");
		end)

	end)

	return;

else

	CreateClientConVar("namesonhead_textsize", 50, true, false)
	CreateClientConVar("namesonhead_flagsize", 3, true, false)
	CreateClientConVar("namesonhead_mode", 1, true, false)
	local fontSize = GetConVarNumber("namesonhead_textsize") or 50

	local function loadFont(m_size)

		surface.CreateFont("NamesOnHead", {
			font = "bankgthd",
			size = m_size
		});	

	end

	local function repeatFunc()

		local country = string.lower(system.GetCountry() or "")

		if(LocalPlayer():GetNWString("country") == "" and country!="") then

			net.Start("sendCountry")
				net.WriteString(country)
			net.SendToServer();

			timer.Simple(5, repeatFunc)

		end

	end

	local me = LocalPlayer();

	local function initializeNamesOnHead()

		me = LocalPlayer();
		fontSize = GetConVarNumber("namesonhead_textsize") or 50
		loadFont(fontSize)
		repeatFunc()

	end
	hook.Add("Initialize", "NamesOnHead", initializeNamesOnHead);

	local function paintNamesOnHead3D()

		if(!me or !IsValid(me)) then 

			me = LocalPlayer();
			return 

		end

		surface.SetFont("NamesOnHead");

		
		local ang = me:EyeAngles()
			ang:RotateAroundAxis(ang:Up(), -80)
			ang:RotateAroundAxis(ang:Forward(), 90)
			ang:RotateAroundAxis(ang:Right(), 0)
		

		for k,v in pairs(player.GetAll()) do
			
			if(v == me) then 
				continue 
			end

			local country = v:GetNWString("country");
			local pos = Vector(0,0,0)
			local material;

			if(not country or country == "") then
				material = Material("icon16/help.png");
			else
				material = Material("materials/countryicons16/"..country..".png");
			end
			local textWidth = surface.GetTextSize(v:Name())

			cam.Start3D2D(v:GetPos()+Vector(0,0,80), ang, 0.1);

				surface.SetMaterial(material);
				surface.SetDrawColor(Color(255,255,255,alpha))
				local flagSize = GetConVarNumber("namesonhead_flagsize") or 3
				surface.DrawTexturedRect( pos.x-textWidth/2-16*(flagSize+1), pos.y-11*(flagSize/2)-20	, 16*flagSize ,11*flagSize)

				local colorText = team.GetColor(v:Team())

				draw.SimpleTextOutlined(v:Name(), "NamesOnHead", pos.x, pos.y-20, colorText, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2,Color(0,0,0, alpha) )
				
			cam.End3D2D();

		end
	end
	local function paintNamesOnHeadHud()

		if(!me or !IsValid(me)) then 

			me = LocalPlayer();
			return 

		end

		surface.SetFont("NamesOnHead");

		for k,v in pairs(player.GetAll()) do
			
			if(v == me) then 
				continue 
			end

			local country = v:GetNWString("country");
			local pos = (v:GetPos() + Vector(0,0,80)):ToScreen()
			local material;

			if(not country or country == "") then
				material = Material("icon16/help.png");
			else
				material = Material("materials/countryicons16/"..country..".png");
			end
			local textWidth = surface.GetTextSize(v:Name())


			local alpha = 255-math.Clamp(v:GetPos():Distance(me:GetPos())*0,255,127)

			surface.SetMaterial(material);
			surface.SetDrawColor(Color(255,255,255,alpha))
			local flagSize = GetConVarNumber("namesonhead_flagsize") or 3
			surface.DrawTexturedRect( pos.x-textWidth/2-16*(flagSize+1), pos.y-11*(flagSize/2)-20	, 16*flagSize ,11*flagSize)

			local colorText = team.GetColor(v:Team())
			colorText.a = alpha

			draw.SimpleTextOutlined(v:Name(), "NamesOnHead", pos.x, pos.y-20, colorText, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2,Color(0,0,0, alpha) )
			

		end
	end


	local lastMode = nil
	hook.Add("Think", "NamesOnHead", function()

		local curFontSize = GetConVarNumber("namesonhead_textsize") or 50
		if(curFontSize ~= fontSize) then
			
			loadFont(curFontSize)

		end

		local curMode = GetConVarNumber("namesonhead_mode") or 0
		if(lastMode ~= curMode) then
			if(curMode== 1) then

				hook.Add("PostDrawTranslucentRenderables", "NamesOnHead", paintNamesOnHead3D);
				hook.Remove("HUDPaint", "NamesOnHead")

			else

				hook.Add("HUDPaint", "NamesOnHead", paintNamesOnHeadHud);
				hook.Remove("PostDrawTranslucentRenderables", "NamesOnHead")

			end
			lastMode = curMode
		end
	end)

end

Screenshots: In few min

Before actually drawing it check if player is alive:

if ply:Alive() then

I have heard that Alive() is kinda slow, so you can just use:

if ply:Health() < 1 then

And for invisible i guess you are talking about “ulx cloak”?

you can use this then:

if ply:GetMaterial() == “models/effects/vol_light001” then

because “ulx cloak” sets players model to that, but there might be a better way on that, this one still works.