evasivedocs
CS2 scriptingLua 5.4.9 · 25 guides · 292 API entries
Tutorials

Reading players and entities

In this tutorial you build name tags: each enemy gets their name above the head and a health bar beside their box. Then you add damage numbers that float up where a player got hit.

Everything here needs Allow game scripting, which is on by default. The game data is read only. A script can look, it cannot change anything in the game.

1. Are we in a match?

game and entities are nil when the permission is off, and the lists are empty when you are not in a match. Start every game script with a guard:

Lua
events.on("paint", function()
    if not game or not game.connected() then return end
    local enemies = entities.players("enemies")
    draw.text(20, 20, #enemies .. " enemies listed", color(255, 255, 255))
end)

entities.players(filter) lists the players the game is sending you, far to near. The filter is "enemies", "teammates", "alive" or nothing for everyone. Players outside the area the game updates you about, called dormant, are not in the list.

2. A name over each head

p:bone_screen(bone.head) gives the screen position of the head, or nil when it is off screen:

Lua
events.on("paint", function()
    if not game or not game.connected() then return end
    for _, p in ipairs(entities.players("enemies")) do
        if p.alive then
            local x, y = p:bone_screen(bone.head)
            if x then
                draw.text(x, y - 18, p.name, color(255, 255, 255), {align = "center", font = "esp", outline = true})
            end
        end
    end
end)

3. A health bar

p:box() gives the same screen box the ESP uses, as two corners. Put a bar on its left edge, as tall as the player's health:

Lua
-- @name Name tags
-- @description Names over heads and a health bar for each enemy.

local show_team = ui.checkbox("Teammates too", false)

local function health_bar(p)
    local x1, y1, x2, y2 = p:box()
    if not x1 then return end
    local frac = math.clamp(p.health / 100, 0, 1)
    local bar = (y2 - y1) * frac
    draw.rect_filled(x1 - 6, y1 - 1, 4, y2 - y1 + 2, color(0, 0, 0, 170))
    draw.rect_filled(x1 - 5, y2 - bar, 2, bar, color.lerp(color(220, 60, 60), color(120, 220, 90), frac))
end

events.on("paint", function()
    if not game or not game.connected() then return end
    for _, p in ipairs(entities.players(show_team:get() and "alive" or "enemies")) do
        if p.alive and not p.is_local then
            local x, y = p:bone_screen(bone.head)
            if x then
                local col = p.is_enemy and color(255, 255, 255) or color(120, 200, 255)
                draw.text(x, y - 18, p.name, col, {align = "center", font = "esp", outline = true})
            end
            health_bar(p)
        end
    end
end)

color.lerp blends red into green as health goes up. math.clamp keeps the fraction between 0 and 1.

4. Damage numbers

Game events hand you what happened. player_hurt fires when a listed player loses health, with the position and the damage. Keep the hits in a table, and in paint draw each one rising for a second:

Lua
local hits = {}

events.on("player_hurt", function(e)
    hits[#hits + 1] = {pos = e.pos, text = "-" .. e.damage, born = time.now()}
end)

events.on("paint", function()
    if not game then return end
    local now = time.now()
    for i = #hits, 1, -1 do
        local h = hits[i]
        local age = now - h.born
        if age > 1 then
            table.remove(hits, i)
        else
            local x, y = game.world_to_screen(h.pos)
            if x then
                local alpha = math.floor(255 * (1 - age))
                draw.text(x, y - 40 * age, h.text, color(255, 90, 90, alpha), {align = "center", outline = true})
            end
        end
    end
end)

game.world_to_screen(pos) turns a world position into screen pixels with the same camera the ESP uses. It gives nil for points behind you. Walking the list backwards lets you remove entries while you loop.

What a player has

A few of the fields on a player. The API reference lists all of them under Player.

Field Type Meaning
p.index integer The player's slot. Stays the same while they are in the match.
p.name, p.steam_id string Name and SteamID64.
p.team integer 2 is T, 3 is CT.
p.health, p.armor, p.money, p.ping integer
p.alive, p.is_enemy, p.is_local boolean is_enemy is true for everyone in free for all.
p.scoped, p.flashed, p.defusing, p.ducked, p.on_ground boolean
p.helmet, p.defuser boolean
p.origin, p.eye_pos, p.center vec3 Feet, eyes and body centre.
p.velocity, p.view_angles vec3 Units per second, and pitch, yaw, roll in degrees.
p.distance number Distance to the camera in game units.
p.weapon table name, type, ammo, max_ammo, reloading and more.

And the methods:

Method Gives
p:box() x1, y1, x2, y2 on screen, or nil.
p:screen() Screen position of the feet, or nil.
p:bone(id) and p:bone_screen(id) A bone in the world or on screen. Ids are in bone: bone.head, bone.neck, bone.pelvis, bone.hand_l and so on.
p:is_visible() A line of sight check against the map from your eyes.
p:hitboxes() The hitbox capsules aim and trigger use.

p.visible and p:is_visible() are not the same. The field is the game's own radar spotted flag. The method traces the map from your eyes, and it counts toward the 256 map rays a script gets per update. Past that it gives nil and "ray budget".

p.weapon.type matches the values in weapon_type:

Lua
local function is_sniper(p)
    return p.weapon.type == weapon_type.sniper
end

You

entities.local_player() gives your own player, or nil when there is none. It has its own fields, like me.alive, me.health, me.weapon, me.shots_fired, me.flash_alpha and, while you are dead, me.spectating and me.observer_mode.

Lua
events.on("paint", function()
    if not game then return end
    local me = entities.local_player()
    if not me or not me.alive then return end
    local w = me.weapon
    if w and w.max_ammo and w.max_ammo > 0 then
        local sw, sh = draw.screen_size()
        draw.text(sw / 2, sh / 2 + 30, w.ammo .. " / " .. w.max_ammo, color(255, 255, 255), {align = "center", outline = true})
    end
end)

Drawing in the world

Three draw functions take world positions and project them for you. They need the same permission:

Lua
events.on("paint", function()
    if not game or not game.connected() then return end
    for _, p in ipairs(entities.players("enemies")) do
        if p.alive then
            draw.world_circle(p.origin, 20, color(255, 80, 80, 160))
            draw.world_line(p.eye_pos, p.origin, color(255, 255, 255, 80))
            draw.world_text(p.eye_pos, tostring(p.health), color(255, 255, 255))
        end
    end
end)

World positions are vec3 values. You can add, subtract and scale them, and they have :length(), :dist(other), :normalize() and :dot(other):

Lua
local function distance_between(a, b)
    return a.origin:dist(b.origin)
end

local above_head = vec3(0, 0, 12)
local function label_pos(p)
    return p.eye_pos + above_head
end

Everything else in the match

Function Gives
entities.bomb() The C4: planted, ticking, time_left, defuse_left, being_defused, site, origin and more. Nil when there is no bomb.
entities.projectiles() Grenades in flight and burning: kind, origin, remaining and more.
entities.items() Weapons on the ground: name, origin, ammo.
entities.spectators() Who is spectating: name, mode, watching_local.
entities.dormant() Alive players out of view, with their last known position.
game.map() The map name.
game.camera() The camera: origin, angles, fov.
game.freeze_period() True during freezetime.
game.ffa() True in free for all.

The examples use several of these.

Keep the index, not the player

A player object is only valid in the frame it was listed in. Keeping one for later raises stale player. Store p.index and look the player up again with entities.player(index). See How scripts run.

Next

Your own settings adds controls so users can tune what your script draws.