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

Spectator list

A panel with everyone who is spectating. Names of people watching you are shown in red.

Needs: Allow game scripting.

Lua
-- @name Spectator list
-- @description Who is spectating, and who is watching you.

local only_me = ui.checkbox("Only people watching me", false)
local warn_col = ui.color("Watching me", color(255, 110, 110))
local pos_y = ui.slider("Y", 0, 100, 25, {suffix = "%"})

events.on("paint", function()
    if not game or not game.connected() then return end

    local names = {}
    for _, s in ipairs(entities.spectators()) do
        if s.watching_local or not only_me:get() then
            names[#names + 1] = {name = s.name, me = s.watching_local}
        end
    end
    if #names == 0 then return end

    local sw, sh = draw.screen_size()
    local w = 180
    local h = 22 + #names * 16 + 6
    local x = sw - w - 20
    local y = math.floor((sh - h) * pos_y:get() / 100)
    local row_y = draw.panel(x, y, w, h, "Spectators")
    for i, n in ipairs(names) do
        local c = n.me and warn_col:get() or color(210, 210, 210)
        draw.text(x + 8, row_y + (i - 1) * 16 + 2, n.name, c, {shadow = true})
    end
end)

How it works

  • entities.spectators() lists everyone spectating, each with name, mode and watching_local.
  • The script first builds its own list of rows, so it knows the panel's height before it draws anything.
  • The panel hides itself when nobody is in the list.

Try this

  • Flash the panel for a moment when the count of people watching you goes up. Keep last frame's count in a local variable and compare.
  • Use the same panel for entities.dormant() to list enemies you lost track of, with since seconds since they were last seen.