Script API - v26.60
    Preparing search index...

    Class ScoreboardObjective

    Contains objectives and participants for the scoreboard.

    export class ScoreboardObjective {
    private constructor();
    readonly displayName: string;
    readonly id: string;
    readonly isValid: boolean;
    addScore(participant: Entity | ScoreboardIdentity | string, scoreToAdd: number): number;
    getParticipants(): ScoreboardIdentity[];
    getScore(participant: Entity | ScoreboardIdentity | string): number | undefined;
    getScores(): ScoreboardScoreInfo[];
    hasParticipant(participant: Entity | ScoreboardIdentity | string): boolean;
    removeParticipant(participant: Entity | ScoreboardIdentity | string): boolean;
    setScore(participant: Entity | ScoreboardIdentity | string, score: number): void;
    }
    Index
    displayName: string

    Returns the player-visible name of this scoreboard objective.

    This property can throw when used.

    This property can't be read in early-execution mode.

    id: string

    Identifier of the scoreboard objective.

    This property can throw when used.

    This property can't be read in early-execution mode.

    isValid: boolean

    Returns true if the ScoreboardObjective reference is still valid.

    This property can't be read in early-execution mode.

    • Parameters

      • participant: string | Entity | ScoreboardIdentity

        Participant to apply the scoreboard value addition to.

      • scoreToAdd: number

      Returns number

      Adds a score to the given participant and objective.

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

      This function can throw errors.

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

      import { world } from "@minecraft/server";

      const money = world.scoreboard.getObjective("money");
      const player = world.getPlayers()[0];
      const currentScore = money.addScore(player, 100);
      player.sendMessage(`Your current score is ${currentScore}`);
      import { world } from "@minecraft/server";

      const money = world.scoreboard.getObjective("money");
      const player = world.getPlayers()[0];
      const currentScore = money.addScore(player, -100);
      player.sendMessage(`Your current score is ${currentScore}`);
    • Parameters

      Returns number | undefined

      Returns a specific score for a participant.

      This function can throw errors.

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

    • Returns ScoreboardScoreInfo[]

      Returns specific scores for this objective for all participants.

      This function can throw errors.

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

      import { Player, system, world } from "@minecraft/server";

      // Run `/scriptevent example:topplayer` in chat to display the top money scores.
      system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
      if (id !== "example:topplayer" || !(sourceEntity instanceof Player)) return;

      const money = world.scoreboard.getObjective("money");
      if (!money) {
      sourceEntity.sendMessage("Scoreboard objective 'money' does not exist.");
      return;
      }

      const scoreArray = money.getScores();
      scoreArray.sort((a, b) => b.score - a.score);

      const topNames = scoreArray.map((entry) => `${entry.participant.displayName}: ${entry.score}`);
      sourceEntity.sendMessage(`Leaderboard: ${topNames.join(", ")}`);
      });
    • Parameters

      Returns boolean

      Returns if the specified identity is a participant of the scoreboard objective.

      This function can throw errors.

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

    • Parameters

      Returns void

      Sets a score for a participant.

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

      This function can throw errors.

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

      import { world } from "@minecraft/server";

      const money = world.scoreboard.getObjective("money");
      const player = world.getPlayers()[0];
      money.setScore(player, 0);
      player.sendMessage(`Your score has been reset.`);