Hover Player Names?

So well im just triying to do 2 simple things

1 draw the player name like 1 cm above his head pretty much like what you see in cod modern warfare 2

2 draw a NPC Name above his head once he is spawned

since i never worked with hud only with DERMA any hints are very appreciated!

Maybe THIS could help you
Have a look at example 2

[lua]
function NamesOverPlayers()

for _, pl in pairs(player.GetAll()) do
	if pl:IsValid() then
		
		local pos = (pl:GetPos() + Vector(0,0,75)):ToScreen()
		
		draw.SimpleText(pl:Nick(),"TabLarge",pos.x,pos.y,team.GetColor(pl:Team()),TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER)
		
	end
end

end
hook.Add(“HUDPaint”, “Draw names over players”, NamesOverPlayers)
[/lua]
Do pretty much the same for the npc’s.

thanks!

okay well i tried that i hooked it to the PlayerSpawn on my init.lua and then i get \init.lua:28] attempt to call method ‘ToScreen’ (a nil value)(Hook: PlayerSpawn)

The code is supposed to be linked to HUDPaint, pretty much anything else will throw up an error as you are supposed to be painting names onto your screen

I’m guessing that you only want the names to be seen when somone is alive then?

yes once they dead the name should Dissapear

Perform an if ( ply:Alive() ) then check to ensure they are alive before drawing it.

well im stuck applying this to NPC since they dont use nick or steamid how can i draw a custom name for them?

[lua]draw.DrawText(“Insert Name Here”, “TargetID”, pos.x, pos.y + 81, Color(255, 255, 255,200), 1)[/lua]

This MIGHT WORK

[lua]local npcname = string.Right(npc:GetClass(),#npc:GetClass()-4)[/lua]
Removes “npc_” from the npc’s classname. So “npc_zombie” will become “zombie”.

so well the whole code now is

function NamesOverPlayers()
local npcname = string.Right(npc:GetClass(),#npc:GetClass()-4)
for _, pl in pairs(player.GetAll()) do
if pl:IsValid() then

        local pos = (pl:GetPos() + Vector(0,0,75)):ToScreen()
         
        draw.SimpleText(npcname(),"TabLarge",pos.x,pos.y,team.GetColor(pl:Team()),TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER)
         
    end
end

end
hook.Add(“HUDPaint”, “Draw names over players”, NamesOverPlayers)

function HUDPaint()

end

and it still dont work

in your hook, I don’t think you can have spaces in the name. So try:
[lua]hook.Add( “HUDPaint”, “DrawNamesOverPlayers”, NamesOverPlayers )[/lua]

And make sure you’re running this clientside(aka cl_init).

There’s much more than that.

[lua]
function NamesOverPlayersAndNpcs()

for _, ent in pairs(ents.GetAll()) do -- Get all the entities on the server.
			
	if ent:IsValid() and ent:IsPlayer() or ent:IsNPC() then -- If the entity is valid and is an npc or a player.
	
	local pos = (ent:GetPos() + Vector(0,0,75)):ToScreen() -- Get the position of the entity and convert it to screen cordinates.
			
		if ent:IsPlayer() then -- If the entity is a player
			draw.SimpleText(ent:Nick(),"TabLarge",pos.x,pos.y,team.GetColor(ent:Team()),TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER)  -- Draw it.
		else -- Else if the entity is an npc, since we only search for npc's and players.
			local npcname = string.Right(ent:GetClass(),#ent:GetClass()-4) -- Here we make a name out of the npc's classname.
			draw.SimpleText(npcname,"TabLarge",pos.x,pos.y,Color(255,255,0,255),TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER) -- Draw it.
		end

	end
			
end

end
hook.Add(“HUDPaint”, “Draw names over players and npcs”, NamesOverPlayersAndNpcs)
[/lua]

thanks man

Somewhat stolen from darkrp
[lua]
//cl_init.lua
local function DrawPlayerInfo( ply )
if not ply:Alive() then return end
local pos = ply:EyePos()
pos.z = pos.z + 34
pos = pos:ToScreen()

draw.DrawText(ply:Nick(), "TargetID", pos.x + 1, pos.y + 1, Color(0, 0, 0, 255), 1)
draw.DrawText(ply:Nick(), "TargetID", pos.x, pos.y, team.GetColor(ply:Team()), 1)
draw.DrawText("Health: " ..ply:Health(), "TargetID", pos.x + 1, pos.y + 21, Color(0, 0, 0, 255), 1)
draw.DrawText("Health: " ..ply:Health(), "TargetID", pos.x, pos.y + 20, Color(255,255,255,200), 1)

end

hook.Add(“HUDPaint”, “PlayerNames”, function()
local tr = LocalPlayer():GetEyeTrace()
if tr.Entity and tr.Entity.IsValid and tr.Entity:IsPlayer() and tr.Entity:GetPos():Distance(LocalPlayer():GetPos()) < 400 then
local pos = {x = ScrW()/2, y = ScrH() / 2}
DrawPlayerInfo(tr.Entity)
end
end)
[/lua]