Skip to content

@minecraft/server


@minecraft/server / Scoreboard

Class: Scoreboard ​

Defined in: @minecraft/server/index.d.ts:19000

Contains objectives and participants for the scoreboard.

Example ​

updateScoreboard.ts

typescript
import { world, DisplaySlotId, ObjectiveSortOrder, DimensionLocation } from '@minecraft/server';

function updateScoreboard(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
  const scoreboardObjectiveId = 'scoreboard_demo_objective';
  const scoreboardObjectiveDisplayName = 'Demo Objective';

  const players = world.getPlayers();

  // Ensure a new objective.
  let objective = world.scoreboard.getObjective(scoreboardObjectiveId);

  if (!objective) {
    objective = world.scoreboard.addObjective(scoreboardObjectiveId, scoreboardObjectiveDisplayName);
  }

  // get the scoreboard identity for player 0
  const player0Identity = players[0].scoreboardIdentity;

  if (player0Identity === undefined) {
    log('Could not get a scoreboard identity for player 0.');
    return -1;
  }

  // initialize player score to 100;
  objective.setScore(player0Identity, 100);

  world.scoreboard.setObjectiveAtDisplaySlot(DisplaySlotId.Sidebar, {
    objective: objective,
    sortOrder: ObjectiveSortOrder.Descending,
  });

  const playerScore = objective.getScore(player0Identity) ?? 0;

  // score should now be 110.
  objective.setScore(player0Identity, playerScore + 10);
}

Methods ​

addObjective() ​

addObjective(objectiveId, displayName?): ScoreboardObjective

Defined in: @minecraft/server/index.d.ts:19049

Parameters ​

objectiveId ​

string

displayName? ​

string

Returns ​

ScoreboardObjective

Remarks ​

Adds a new objective to the scoreboard.

This function can't be called in restricted-execution mode.

Throws ​

This function can throw errors.

Example ​

updateScoreboard.ts

typescript
import { world, DisplaySlotId, ObjectiveSortOrder, DimensionLocation } from '@minecraft/server';

function updateScoreboard(log: (message: string, status?: number) => void, targetLocation: DimensionLocation) {
  const scoreboardObjectiveId = 'scoreboard_demo_objective';
  const scoreboardObjectiveDisplayName = 'Demo Objective';

  const players = world.getPlayers();

  // Ensure a new objective.
  let objective = world.scoreboard.getObjective(scoreboardObjectiveId);

  if (!objective) {
    objective = world.scoreboard.addObjective(scoreboardObjectiveId, scoreboardObjectiveDisplayName);
  }

  // get the scoreboard identity for player 0
  const player0Identity = players[0].scoreboardIdentity;

  if (player0Identity === undefined) {
    log('Could not get a scoreboard identity for player 0.');
    return -1;
  }

  // initialize player score to 100;
  objective.setScore(player0Identity, 100);

  world.scoreboard.setObjectiveAtDisplaySlot(DisplaySlotId.Sidebar, {
    objective: objective,
    sortOrder: ObjectiveSortOrder.Descending,
  });

  const playerScore = objective.getScore(player0Identity) ?? 0;

  // score should now be 110.
  objective.setScore(player0Identity, playerScore + 10);
}

clearObjectiveAtDisplaySlot() ​

clearObjectiveAtDisplaySlot(displaySlotId): ScoreboardObjective | undefined

Defined in: @minecraft/server/index.d.ts:19057

Parameters ​

displaySlotId ​

DisplaySlotId

Returns ​

ScoreboardObjective | undefined

Remarks ​

Clears the objective that occupies a display slot.

This function can't be called in restricted-execution mode.


getObjective() ​

getObjective(objectiveId): ScoreboardObjective | undefined

Defined in: @minecraft/server/index.d.ts:19065

Parameters ​

objectiveId ​

string

Identifier of the objective.

Returns ​

ScoreboardObjective | undefined

Remarks ​

Returns a specific objective (by id).


getObjectiveAtDisplaySlot() ​

getObjectiveAtDisplaySlot(displaySlotId): ScoreboardObjectiveDisplayOptions | undefined

Defined in: @minecraft/server/index.d.ts:19072

Parameters ​

displaySlotId ​

DisplaySlotId

Returns ​

ScoreboardObjectiveDisplayOptions | undefined

Remarks ​

Returns an objective that occupies the specified display slot.


getObjectives() ​

getObjectives(): ScoreboardObjective[]

Defined in: @minecraft/server/index.d.ts:19078

Returns ​

ScoreboardObjective[]

Remarks ​

Returns all defined objectives.


getParticipants() ​

getParticipants(): ScoreboardIdentity[]

Defined in: @minecraft/server/index.d.ts:19084

Returns ​

ScoreboardIdentity[]

Remarks ​

Returns all defined scoreboard identities.


removeObjective() ​

removeObjective(objectiveId): boolean

Defined in: @minecraft/server/index.d.ts:19093

Parameters ​

objectiveId ​

string | ScoreboardObjective

Returns ​

boolean

Remarks ​

Removes an objective from the scoreboard.

This function can't be called in restricted-execution mode.

Throws ​

This function can throw errors.


setObjectiveAtDisplaySlot() ​

setObjectiveAtDisplaySlot(displaySlotId, objectiveDisplaySetting): ScoreboardObjective | undefined

Defined in: @minecraft/server/index.d.ts:19107

Parameters ​

displaySlotId ​

DisplaySlotId

objectiveDisplaySetting ​

ScoreboardObjectiveDisplayOptions

Returns ​

ScoreboardObjective | undefined

Returns the previous ScoreboardObjective set at the display slot, if no objective was previously set it returns undefined.

Remarks ​

Sets an objective into a display slot with specified additional display settings.

This function can't be called in restricted-execution mode.

Throws ​

This function can throw errors.