Text above players heads when they're close enough and you look at them

Hi. How do I make a script that shows player’s names and health above their heads when I look at them and they’re close enough?

[lua]local dist = 500
local colour = Color(105, 105, 205)
hook.Add(“HUDPaint”, “PlayerNames”, function()
local ent = LocalPlayer():GetEyeTrace().Entity
if ent and ent:IsValid() and ent:IsPlayer() then
local pos = ent:GetPos()
if (LocalPlayer():GetPos() - pos):LengthSqr() <= dist^2 then
local health = ent:Health()
local name = ent:Nick()
pos = pos:ToScreen()
draw.DrawText(name … " " … health, “ScoreboardText”, pos.x, pos.y, colour, TEXT_ALIGN_CENTER)
end
end
end)[/lua]

Put that in autorun/client/whatever.lua

You can change the colour and distance values at the top.

This is directly from my roleplay gamemode and works perfectly, It draws text above there head if your close to them and looking at them.

[lua]
surface.CreateFont (“Old London”, 30, 700, true, false, “NameFont”) --unscaled
surface.CreateFont (“Old London”, 25, 400, true, false, “DescriptFont”) --unscaled

function GM:HUDPaint( )
if( ValidEntity( LocalPlayer() ) ) then
local trace = { }
trace.start = LocalPlayer():EyePos();
trace.endpos = trace.start + LocalPlayer():GetAimVector() * 300;
trace.filter = LocalPlayer();

	local tr = util.TraceLine( trace );
	
	if( ValidEntity( tr.Entity ) and tr.Entity:IsPlayer() ) then
	
				local pos = tr.Entity:GetPos()
		pos.z = tr.Entity:LocalToWorld(tr.Entity:OBBMaxs()).z
		pos = pos:ToScreen()

		draw.SimpleText( tr.Entity:GetNWString("Name"), "NameFont", pos.x, pos.y - 50, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
		draw.SimpleText( tr.Entity:GetNWString("Description"), "DescriptFontt", pos.x, pos.y - 25, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
	end
	
	if( ValidEntity( tr.Entity ) and tr.Entity:GetClass() == "Elipse_Gold" ) then
		
		local pos = tr.Entity:GetPos()
		pos.z = tr.Entity:LocalToWorld(tr.Entity:OBBMaxs()).z
		pos = pos:ToScreen()

		draw.SimpleText( tostring(tr.Entity:GetNWInt("Amount")), "NameFont", pos.x, pos.y - 50, Color(255,255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
	end
end

end
[/lua]

[lua]COLOR = Color(0, 0, 0, 255) – choose what color the text should be here.

local enabled = CreateClientConVar(“names_enabled”, 1, true, false)
local distance = CreateClientConVar(“names_distance”, 999999, true, false)

local function NamesOnHead()
if enabled:GetInt() == “1” then
for k, v in pairs(player.GetAll()) do
local pos = v:GetPos():ToScreen()
if (v:GetPos() - LocalPlayer():GetPos()):Length() <= distance:GetInt() then
draw.SimpleText(v:Nick(), “ScoreboardText”, pos.x, pos.y - 21, COLOR, TEXT_ALIGN_CENTER)
end
end
end
end
hook.Add(“HUDPaint”, “names”, NamesOnHead)[/lua]

same file path as makers, mine checks the convar number so if you want the distance to be 10 set the convar names_distance to 10. I figured it would be easier for you to integrate into a DNumSlider.

By the way, found this on the wiki:

[lua]
surface.CreateFont ( “coolvetica”, 40, 400, true, false, “CV20”, true )

function DrawName( ply )

if !ply:Alive() then return end

local offset = Vector( 0, 0, 85 )
local ang = LocalPlayer():EyeAngles()
local pos = ply:GetPos() + offset + ang:Up()

ang:RotateAroundAxis( ang:Forward(), 90 )
ang:RotateAroundAxis( ang:Right(), 90 )

cam.Start3D2D( pos, Angle( 0, ang.y, 90 ), 0.25 )
	draw.DrawText( ply:GetName(), "CV20", 2, 2, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER )
cam.End3D2D()

end
hook.Add( “PostPlayerDraw”, “DrawName”, DrawName )
[/lua]

I personally don’t think that using 3D2D cams are worth using for something that is this simple.

A feature using this is in the newer version of Tiramisu, it’s very cool.