Players and returned tables

The player table

Every event and game.players() hands out player tables. A player table is a snapshot taken when it was handed out: its fields do not change afterwards. To read a player again later, call game.player(id). A table has a metatable that gives it the methods below, which act on the live player through its id; calling one on a player who has left raises that player has left.

Positions use map coordinates: x, y along the ground, z growing downwards, and the player’s z is its hip (the floor under a standing player is about z + 2.4).

Fields

NameTypeMeaning
p.idintegerThe player's id, 0 to 255. Unique while connected; the key to use in tables.
p.namestringThe name. Bots are `Bot_` plus two words.
p.teaminteger1, 2, or 0 for a spectator.
p.alivebooleanIn the world, not waiting to respawn.
p.hpintegerHealth, 0 to 100 (a class with more health is scaled to this).
p.killsintegerKills this match.
p.deathsintegerDeaths this match.
p.assistsintegerKill assists (modes that count them).
p.scoreintegerThe personal score column (what `p:set_score` writes).
p.capturesintegerIntel captures.
p.shotsintegerShots fired.
p.hitsintegerShots that hit a player.
p.blocks_placedintegerBlocks built.
p.squadintegerIts squad, 1 to 16, 0 for none.
p.reloadingbooleanReloading now.
p.crouchingbooleanCrouched.
p.sprintingbooleanSprinting.
p.airbornebooleanNot on the ground.
p.wadebooleanIn water.
p.jetpackbooleanWearing a jetpack this life.
p.botbooleanA server-side bot.
p.goalstringBots only: the name of the goal it follows.
p.targetintegerBots only, and only while it has one: the id of the enemy it fights.
p.toolintegerThe tool in hand: 0 spade, 1 block, 2 gun, 3 grenade.
p.weaponinteger0 rifle, 1 SMG, 2 shotgun.
p.blocksintegerBlocks carried.
p.grenadesintegerGrenades carried.
p.intelbooleanCarrying the enemy intel.
p.classstring1.0 only, and only in a mode with classes: `commando`, `rocketeer`, `marksman` or `miner`. Absent otherwise.
p.ipstringHumans only: its address as text (an IPv6 player's full address). Absent for bots.
p.xnumberEast-west position of the hip.
p.ynumberNorth-south position of the hip.
p.znumberHeight of the hip, growing downwards.
p.oxnumberWhere the player looks: x of a unit vector.
p.oynumbery of the look vector.
p.oznumberz of the look vector (positive looks down).
local plugin = {}
function plugin.on_player_tick(p)
    if p.alive and not p.bot and p.z < 5 then
        p:message(("You are very high: z=%.1f"):format(p.z))
    end
end
return plugin

Methods

Methods are called with a colon: p:message("hi"). Unless said otherwise they return nothing.

p:message
both profiles verified by running
p:message(text)

A line only this player reads. Nothing happens for a bot.

local p = game.player(0)
if p then p:message("Hello") end

p:set_hp
both profiles verified by running
p:set_hp(n)

Sets the health (capped at 100). Zero or less kills the player. Nothing happens to a dead player.

local p = game.player(0)
if p then p:set_hp(50) end

p:damage
both profiles verified by running
p:damage(n, by)

Hurts the player through the normal path: hit marks, kill credit and the kill feed go to by, a player table (default: the player itself). n must be above zero, an integer of health points.

local victim, attacker = game.player(1), game.player(2)
if victim and attacker then victim:damage(25, attacker) end

p:kill
both profiles verified by running
p:kill()

Kills the player (reason 5 on on_kill). Nothing happens to a dead player.

local p = game.find("Bot_Dusty")
if p then p:kill() end

p:teleport
both profiles verified by running
p:teleport(x, y, z)

Moves the player to the map coordinates of the hip, its own client included; its velocity is zeroed. Off the map raises bad argument ... off the map. Logged as EVT plugin action=teleport.

local p = game.player(0)
if p then
    local x, y = game.spawn(1)
    p:teleport(x, y, game.ground(math.floor(x), math.floor(y)) - 3)
end

p:spawn_heli
protocol 1.0 only verified by running
p:spawn_heli() -> entity id | nil, why

A helicopter on dry ground in front of the player, of the player’s team (team 1 for a spectator). It is not a base slot, so nothing replaces a wreck: ask again. Returns the entity’s id, or nil and "profile", "no_ground" or "too_many_objects".

local p = game.player(0)
if p then
    local id, why = p:spawn_heli()
    if not id then p:message("No helicopter: " .. why) end
end

p:restock
both profiles verified by running
p:restock()

Full health, blocks, grenades and ammunition, as at a tent. Nothing happens to a dead player.

function plugin_on_spawn_example(p) p:restock() end

p:fog
both profiles verified by running
p:fog(r, g, b)

This player’s sky colour only (each 0 to 255); game.fog sets everyone’s.

local p = game.player(0)
if p then p:fog(80, 0, 0) end

p:drop_crate
protocol 1.0 only verified by running
p:drop_crate(kind) -> true | false, reason

A supply crate, kind "ammo" or "health", falls from the sky onto the player, as a reward. The first player at it who needs it takes it; it goes after 2 minutes on the ground. false with "too many crates out, or no ground there" (16 at most). Another kind raises an error.

local p = game.player(0)
if p then p:drop_crate("health") end

p:set_outfit
protocol 1.0 only verified by running
p:set_outfit(n [, colour]) -> true | false, reason

How the player looks (the aosprotocol draft’s numbers: 0 soldier, 7 miner, 64 zombie, 255 its team’s). colour is 0 or 1 for a team’s colour, or 255 (default) for its own. false with "outfit: 0..255". Cosmetic only: the rules never read it.

local p = game.player(0)
if p then p:set_outfit(64) end

p:outfit
protocol 1.0 only verified by running
p:outfit() -> number | nil, reason

What the player is dressed in (255 means its team’s outfit).

local p = game.player(0)
if p then game.log("outfit " .. tostring(p:outfit())) end

p:has_role
both profiles verified by running
p:has_role(role) -> boolean

true when the player is logged in as that role or a higher one: "trusted", "guard", "mod", "admin", "manager".

for _, p in ipairs(game.players()) do
    if p:has_role("admin") then p:message("You are an admin") end
end

p:sees
both profiles verified by running
p:sees(q) -> boolean

true when there is a clear line from this player’s eye to q’s chest (the bots’ own sight test, for any player). q is a player table. No range limit other than the map.

local a, b = game.player(0), game.player(1)
if a and b and a:sees(b) then game.log("a sees b") end

p:kick
both profiles verified by running
p:kick([reason])

Disconnects the player and tells everyone Player X (#id) has been kicked: reason. A bot cannot be kicked (raises a bot cannot be kicked): remove bots with /kickbot. Logged as EVT plugin action=kick.

local p = game.find("Spammer")
if p and not p.bot then p:kick("spamming") end

p:set_class
protocol 1.0 only verified by running
p:set_class(name_or_n) -> boolean

Picks the class for the player, as its class screen does (a living player respawns with it). false when refused (a class turned off, or the mode has none). on_class is not asked for this call.

local p = game.player(0)
if p and not p:set_class("miner") then p:message("No miner for you") end

p:set_team
both profiles verified by running
p:set_team(team) -> boolean

Moves the player to 0 (spectators), 1 or 2, through the same path as ngspades-client’s request; the mode’s rules still apply (a zombie does not cross). true when it is on that team afterwards, false otherwise. on_team is not asked.

local p = game.player(0)
if p then p:set_team(0) end

p:set_tool
both profiles verified by running
p:set_tool(tool) -> boolean

Puts a tool in hand: 0 spade, 1 block, 2 gun, 3 grenade. false when out of range or the mode forbids it (no gun, no grenade, no blocks). on_tool is not asked.

local p = game.player(0)
if p then p:set_tool(0) end

p:set_weapon
both profiles verified by running
p:set_weapon(weapon) -> boolean

Gives the player a weapon: 0 rifle, 1 SMG, 2 shotgun (refilling to its usual clip); false out of range. ngspades-client shows the new gun at its next update. In a mode with classes the class decides the gun.

local p = game.player(0)
if p then p:set_weapon(2) end

p:set_ammo
both profiles verified by running
p:set_ammo(clip, reserve)

Sets the current clip and the reserve (each clamped to 0 to 255).

local p = game.player(0)
if p then p:set_ammo(30, 90) end

p:set_blocks
both profiles verified by running
p:set_blocks(n)

Sets the blocks carried (0 to 65535); the owner’s client is told.

local p = game.player(0)
if p then p:set_blocks(200) end

p:set_grenades
both profiles verified by running
p:set_grenades(n)

Sets the grenades carried (0 to 255).

local p = game.player(0)
if p then p:set_grenades(3) end

p:set_score
both profiles verified by running
p:set_score(n)

Sets the personal score column of the scoreboard (not the team’s: see game.set_score). Negative becomes 0.

local p = game.player(0)
if p then p:set_score(10) end

p:set_kills
both profiles verified by running
p:set_kills(n)

Sets the kills column.

local p = game.player(0)
if p then p:set_kills(0) end

p:set_deaths
both profiles verified by running
p:set_deaths(n)

Sets the deaths column.

local p = game.player(0)
if p then p:set_deaths(0) end

p:set_assists
both profiles verified by running
p:set_assists(n)

Sets the assists column.

local p = game.player(0)
if p then p:set_assists(0) end

p:set_squad
protocol 1.0 only verified by running
p:set_squad(n) -> boolean

Puts the player in squad n (0 alone, 1 to 16), by the same rules as ngspades-client’s squad command; true when it was taken.

local p = game.player(0)
if p then p:set_squad(1) end

p:set_jetpack
protocol 1.0 only verified by running
p:set_jetpack(on) -> boolean

Grants or removes the jetpack for this life. Returns whether the player has one afterwards (it can stay false when the mode forbids jetpacks: the zombie mode, classic CTF).

local p = game.player(0)
if p and not p:set_jetpack(true) then p:message("No jetpack here") end

Putting it together

A plugin that welcomes players, gives a loadout, and remembers how many times each name has joined:

local plugin = {}

function plugin.on_join(p, team)
    game.store.joins = game.store.joins or {}
    local n = (game.store.joins[p.name] or 0) + 1
    game.store.joins[p.name] = n
    game.store_save()
    p:message(("Welcome back, %s (visit %d)"):format(p.name, n))
end

function plugin.on_spawn(p)
    if not p.bot then
        p:set_blocks(100)
        p:set_grenades(2)
    end
end

return plugin