Skip to content

players.spectate

Has viewer watch a player or a vehicle from a third-person camera they can turn. While they watch, their own character is hidden: it cannot move, be hit or hurt anyone. They keep watching a player who dies, respawns or changes vehicles, and a vehicle whoever drives it, until it is gone. playerSpectateRequest is asked first, and again when the target leaves the server or goes into or out of an interior.

Spectating ends with players.stopSpectating, or on its own when the target is gone, and playerViewEnded says so.

players.spectate(viewer: PlayerTarget, target: SpectateTarget, options?: ViewListenOptions): Promise<ActionResult>
Name Type Description
viewer PlayerTarget The player who watches.
target SpectateTarget Who or what they watch.
options ViewListenOptions Optional. listenAtView: true: the viewer hears nearby chat at what they watch instead of where they started watching.

A promise of an ActionResult: applied once they watch, rejected with a code, or timeout after 21 seconds without an answer.

Code Meaning
view-denied playerSpectateRequest refused it.
target-not-found No such player or vehicle.
target-in-another-world The target is in another virtual world than the viewer.
view-change-pending The viewer is still coming back from their last view; try again in a moment.
resource-unavailable The viewer’s game cannot load what is around the target.
player-not-found No such player, or they left meanwhile.
invalid-value options.listenAtView is not a boolean.
gamemodes/my-mode.ts
import { commands, players } from "nativemp";
commands.register("spec", async (player, args) => {
const target = players.getById(Number(args));
if (target) await players.spectate(player, { kind: "player", player: target });
});