Make text display relative to player, regardless of distance.

I’m trying to write some code that displays text over every player’s head (sort of like names over heads in TTT’s spectator mode), however, I’m having an issue. When the player viewing the text gets further away from the target, the text gets higher and higher above the target’s head. Is there a way to keep the text a certain distance above the target’s head, regardless of the player’s distance to the target?

My code:

for _, target in pairs(player.GetAll()) do
  -- snip
  local initialTextPos = (target:GetPos() + Vector(0, 0, 50)):ToScreen()
  -- AESP.Config.TextOffsetX:GetInt() is probably a value like 0
  local textPosition = Vector(initialTextPos.x + AESP.Config.TextOffsetX:GetInt(),
    -- AESP.Config.TextOffsetY:GetInt() is a value like -50
    initialTextPos.y + AESP.Config.TextOffsetY:GetInt())
  -- snip
  -- This line is just an example of code generated by a helper function
  draw.SimpleTextOutlined("Name: RalphORama", "aesp_font", textPosition.x, textPosition.y, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, color_black)

You can find the full script here: Admin ESP @ Github

I think your issue is that you’re adding the Y offset after you convert the world position to a screen position. The further you get from the player, the smaller the player appears on the screen, but the text will be drawn a constant 50 pixels above the player regardless which causes it to appear like the text is getting further and further away.

That worked! It appears my error was in calculating the z-offset. My new code reads:

local textPosition = (target:GetPos() + Vector(0, 0, AESP.Config.TextOffsetY:GetInt())):ToScreen()