#turns a floating text leaderboard into a message leaderboard

1 messages · Page 1 of 1 (latest)

lime elbow
#

i have the leaderboard code from here, it works fine as a floating text entity. i want to make it work as a command for example !leaderboarddeath will bring up deaths on the leaderboard as a message can anyone help?

#

the code

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

function getObjective(entity) {
    const tag = entity.getTags().find(tag => tag.startsWith("objective:"));
    return tag ? tag.slice(10) : null;
}

function getCustomName(entity) {
    const tag = entity.getTags().find(tag => tag.startsWith("title:"));
    return tag ? tag.slice(6) : null;
}

function getScore(entity, allPlayerNames, leaderboardObjective) {
    const tag = entity.getTags().find(tag => tag.startsWith("scores:")) || "scores:[]";
    const existingScores = JSON.parse(tag.slice(7)).filter(([name]) => !allPlayerNames.includes(name));

    const newScore = world.getAllPlayers().map(oyuncu => {
        try {
            return [oyuncu.name, leaderboardObjective.getScore(oyuncu.scoreboardIdentity)];
        } catch (error) {
            return null;
        }
    }).filter(v => v);

    const score = existingScores.concat(newScore).sort((a, b) => b[1] - a[1]).slice(0, 10);
    entity.removeTag(tag);
    entity.addTag(`scores:${JSON.stringify(score)}`);
    return score;
}

function updateNameTag(entity, customName, scores) {
    if (scores.length === 0) return;
    const nameTag = `${customName || '§cUnnamed'}§r${scores.map(([name, score], i) => `\n§f${i + 1}. §f${name}§r§e §c${score}`).join('')}`;
    entity.nameTag = nameTag;
}

system.runInterval(() => {
    const entities = world.getDimension("overworld").getEntities({ type: 'entity:hologram' });
    for (const entity of entities) {
        const leader = getObjective(entity);
        if (!leader) continue;

        const customName = getCustomName(entity);
        const Objective = world.scoreboard.getObjective(leader);
        const playerNames = world.getAllPlayers().map(oyuncu => oyuncu.name);

        const scores = getScore(entity, playerNames, Objective);
        updateNameTag(entity, customName, scores);
    }
}, 20);
#

it works fine like this

#

i want to make it work as a message

stiff walrus
#

use world.sendMessage() an put the system inside there

#

world.sendMessage(updateNameTag(entity, customName, scores))

#

and make sure the message is a string

#

${updateNameTag(entity, customName, scores)} that should be the string

#

with the ``

stiff walrus
#

@lime elbow How have you done to make the code work that doesn't work for me?

round spindle
#

make sure the player name doesnt change

stiff walrus
#

no, I have an error when I put the tags in objective

stiff walrus
#

Wait i gonna try this out myself.

#

🛌

#

the message just needs the custom Name and scores nothing more. so no nametag will be changed with the send.Message