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.
Signature
Section titled “Signature”players.spectate(viewer: PlayerTarget, target: SpectateTarget, options?: ViewListenOptions): Promise<ActionResult>Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”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. |
Example
Section titled “Example”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 });});Related
Section titled “Related”players.stopSpectatingplayerSpectateRequest,playerViewEndedcamera.attach: follow a target and keep playingplayers.setListenPosition: where a player hears nearby chat