#Leaderboard

1 messages · Page 1 of 1 (latest)

loud birch
#

Hi

#

@unborn reef

#
const { scoreboard } = world
const scoreboard_name = 'luck';

const Top_5_Players_Scores = () => {
    let leaderBoard = scoreboard.getObjective(scoreboard_name)?.getScores()
        .reduce((ar, { participant: { displayName }, score }) => {
            ar.push({
                player: displayName.split('.').pop(),
                score
            });
            return ar;
        }, [])
        .sort((a, b) => a.score - b.score).splice(0, 4)
        .map((entry, index) => (
            `§r§b${index + 1}. §3${entry.player} §b${entry.score}\n`
        ))
    for (; leaderBoard && leaderBoard.length < 3;) {
        leaderBoard.push(`§r§b${leaderBoard.length + 1}. §3$N/A §bN/A\n`)
    }
    return leaderBoard
        ? '§3§lKOTH\n§r§b---------------\n' + leaderBoard.join('') + "§r§b---------------"
        : ''
}
#

I would need the leaderboard to be the one with the most points to be TOP 1 and below fewer points

#

not like that

unborn reef
#
import { world, system } from "@minecraft/server";


const scoreboard = world.scoreboard;
const getObjective = obj => scoreboard.getObjective(obj) ?? scoreboard.addObjective(obj, obj);

function getLeaderBoard(objective, rankingNumber, increasing = true) {
    const sort = increasing ? (a, b) => b.score - a.score : (a, b) => a.score - a.score;
    const board = getObjective(objective)
        .getScores().sort(sort).splice(0, rankingNumber)
        .map(({ participant: { displayName }, score }) => ({ displayName, score }));
    while (board.length < rankingNumber) {
        board.push({ score: 0, displayName: 'N/A' });
    }
    return board;
};


system.runInterval(() => {
    const leaderboard = getLeaderBoard('money', 3)
        .map(({ displayName, score }, place) => `${++place}. ${displayName}: ${score}`).join('\n')
    for (const player of world.getAllPlayers()) {
        player.onScreenDisplay.setActionBar(leaderboard)
    }
});

#

@loud birch

loud birch
#

Thanks bro, de enserió gracias

#

¿Sería "true thanks"? XD no se pero de enserió gracais

unborn reef
#

recopy the runInterval

loud birch
#

Ok

loud birch
unborn reef
#

how do u use setScore?

#

i mean, how do you manage scoreboard for that objective?

loud birch
#

sorry

#

my error

#

The only bad thing is that 0 appears

loud birch
unborn reef
loud birch
unborn reef
#

++place

#

thats basically place + 1

loud birch
#

All look god

unborn reef
#

how do u manage player's score for that?

loud birch
unborn reef
#

i mean how do u use scoreboard players add

loud birch
#

a si

#

/scoreboard players add

unborn reef
#

using script ?

loud birch
# unborn reef using script ?

I don't use a script for the scoreboard, I made some functions that make you rise in rank and points according to your eliminations

unborn reef
#

hmm

loud birch
# unborn reef hmm

When I know more I would like to be able to make fake players to save their scores on the leaderboard

unborn reef
loud birch
loud birch
unborn reef
#
const heading = 'Leaderboard\n<---------->';

system.runInterval(() => {
    const leaderboard = heading + getLeaderBoard('money', 3)
loud birch
loud birch
#

No All commands.scoreboard.players.Etc...

unborn reef
#

im making it rn

loud birch
loud birch
unborn reef
#

yes

loud birch
unborn reef
#

@loud birch give this a try

const scoreboard = world.scoreboard;
const getObjective = obj => scoreboard.getObjective(obj) ?? scoreboard.addObjective(obj, obj);
const displayNamesObj = getObjective('display_names');

world.afterEvents.playerSpawn.subscribe(({ initialSpawn, player }) => {
    let name
    if (!initialSpawn || displayNamesObj.hasParticipant(name = player.name)) return;
    const id = player.scoreboardIdentity?.id
    displayNamesObj.setScore(name, id ?? scoreboard.getParticipants().sort((a, b) => a.id - b.id).pop().id + 1)
    displayNames.set(id, name)
});

const displayNames = new Map(displayNamesObj.getParticipants()
    .reduce((o, { type, id, displayName }) => {
        return type === 'FakePlayer' ? o.concat([id, displayName]) : o
    }, []));
#

and this is the new function```js
function getLeaderBoard(objective, rankingNumber, decreasing = true) {
const sort = decreasing ? (a, b) => b.score - a.score : (a, b) => a.score - b.score;
const board = getObjective(objective)
.getScores().sort(sort).splice(0, rankingNumber)
.map(({ participant: { id }, score }) => ({ displayName: displayNames.get(id), score }));
while (board.length < rankingNumber) {
board.push({ score: 0, displayName: 'N/A' });
}
return board;
};

loud birch
#

@unborn reef

unborn reef
#

@loud birch try

return type === 'FakePlayer' ? [...o,[id, displayName]] : o
loud birch
unborn reef
loud birch
#

o other

loud birch
loud birch
#

Thanks bro ❤️

unborn reef
#

👍bao_foxxo_smile

real mulch
#

still works??

loud birch
wild cave
#

can create this leaderboard like floating text?

unborn reef
#

Yeah...

#

Actually lemme remake this one

west dawn
#

I'm looking for leaderboards like these but they will appear on the name of an entity that has a certain tag

west dawn
unborn reef
#

i suggest learning the basics of javascript 1st before asking for big code

west dawn
#

And a little of how things work, so I can modify it later without breaking the code

unborn reef
#

aight

#

so basically, what i did here is record the player's scoreboardIdentity in an objective full of fakePlayer participants

#

cus fakePlayer type dont change displayname whatsoever

#

so i used that to make a map to get the player's displayname

#

hmm

west dawn
#

Right

#

Thx

unborn reef
#

here's a bit of visual of scoreboardjs const scoreboard = { getObjectives() { return [ { id: "displayNames", displayName: "displayNames", isValid(){}, addScore(){}, setScore(){}, getScore(){}, getScores(){}, hasParticipant(){}, removeParticipant(){}, getParticipants() { return [ { id: 38, type: "FakePlayer", displayName: "Remember M9" }, { id: 90, type: "FakePlayer", displayName: "Remember M3" }, { id: 91, type: "FakePlayer", displayName: "Remember M10" } ] } }, { id: "money", displayName: "§bMoney§r", getParticipants() { return [ { id: 10, type: "Player", displayName: "Remember M9" }, { id: 1, type: "Player", displayName: "Remember M3" }, { id: 27, type: "Player", displayName: "Remember M10" } ] } } ] } }

#

since the id of participant is number, I decided to use that as score for mapping

#

getScores() returns {score: number, participant}

#

array of em

#

then i used that array to make a map of displaynames

#

that score should be the same as the player.scoreboardIdentity.id

#

then to use that map,

const namesMap = new Map(world.scoreboard.getObjective("displayNames").
    getScores().map(o => [o.score, o.participant.displayName]))

const richPlayer = world.scoreboard.getObjective("money")
    .getScores().reduce((top, p) => {
        if (top.score > p.score) return top
        return p
    }, { score: 0 })

const displayName = namesMap.get(richPlayer.participant.id)
world.sendMessage("richest: "+ displayName)
#

if ya'll want to avoid this much code, just use fakeplayer when managing the player's scoreboard

#

so there's no need to make a complex map just to get the participant's displayName