Guide: Building an ESP
Making an overlay: turning world positions into screen positions, drawing boxes and names, and cleaning up.
The Drawing library gives you shapes that draw on top of the game and aren't in the instance tree. No ScreenGui, nothing the game can find in PlayerGui. You make the objects once and move them every frame.
Roblox does the math for you. Camera:WorldToViewportPoint turns a 3D position into a screen position, plus whether it's in front of the camera. The rest is just keeping track of things.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera
local box = Drawing.new("Square")
box.Color = Color3.fromRGB(80, 160, 255)
box.Thickness = 1
box.Filled = false
box.Visible = false
local name = Drawing.new("Text")
name.Color = Color3.new(1, 1, 1)
name.Size = 13
name.Centered = true
name.Outline = true
name.Visible = false
local function update(target)
local character = target.Character
local root = character and character:FindFirstChild("HumanoidRootPart")
if not root then
box.Visible, name.Visible = false, false
return
end
local position, onScreen = camera:WorldToViewportPoint(root.Position)
if not onScreen then
box.Visible, name.Visible = false, false
return
end
-- Scale the box by distance so it tracks the character's apparent size.
local scale = 1 / (position.Z * math.tan(math.rad(camera.FieldOfView * 0.5)) * 2) * 100
local width, height = math.floor(40 * scale), math.floor(60 * scale)
box.Size = Vector2.new(width, height * 2)
box.Position = Vector2.new(position.X - width / 2, position.Y - height)
box.Visible = true
name.Text = target.Name
name.Position = Vector2.new(position.X, position.Y - height - 16)
name.Visible = true
endRenderStepped fires once per frame, so that's where the overlay updates go. Keep the connection so you can disconnect it later.
local connection = RunService.RenderStepped:Connect(function()
for _, player in ipairs(Players:GetPlayers()) do
if player ~= Players.LocalPlayer then
update(player)
end
end
end)Make one object per target, once
The most common ESP bug is calling Drawing.new inside the loop. That makes a new object every frame, never frees the old one, and crashes the client within about a minute. Make your objects first and then move them.
Drawing objects don't get garbage collected while they're visible, because the renderer owns them, not Luau. Remove them yourself and disconnect the loop.
local function teardown()
connection:Disconnect()
box:Remove()
name:Remove()
end
-- A blunt instrument when you have lost track: drops every
-- Drawing object the session has created.
-- cleardrawcache()isrenderobj tells you if something is still a valid Drawing object, and getrenderproperty / setrenderproperty get and set properties by name when you don't know the type.
The Drawing chapter has every object type and its properties, including Line, Circle, Triangle, Quad and Image, plus the fonts in Drawing.Fonts.