#How i can make a Leaderboard

1 messages · Page 1 of 1 (latest)

orchid kernel
#

look at the post I gave you

#

and learn from that

main chasm
#

i dont have any base of this entity
stop

orchid kernel
main chasm
#

and where i put tis code i will not use form

orchid kernel
hallow beaconBOT
#
nelfye

up to you 🤷‍♂️ , get the entity you want to have the leader boards then use the code their

orchid kernel
main chasm
#

Why just don't you let others help me if I can't and you don't want to help me

orchid kernel
main chasm
#

I TRY

orchid kernel
main chasm
#

To begin with, I can't even find the code to make the leaderboard and 2 I don't have the code because I don't know how it works so if you can't help me, go help someone else.

orchid kernel
# main chasm To begin with, I can't even find the code to make the leaderboard and 2 I don't ...
import {system,world} from '@minecraft/server'
system.runInterval(()=>{
  const list = []
  const scores = 
  world.scoreboard.getObjective('Scores').getScores()//Returns a array of scores and participants of that objective
  scores.sort((a,b)=>b.score-a.score) //Sorts it into a array from greatest to least
  for (let i = 0; i<scores.length; i++){
    const score = scores[i]
    list.push(`${score.participant}:${score.score}`)
  }
  const board = list.join('\n')//Joins the array into one string with a new line seperator
  const entity = world.getDimension('overworld').getEntities({tags:['LeaderBoard']}).find(e=>{
    e.hasTag('LeaderBoard')
  })
  entity.nameTag = board
  
})
``` this should get you started
#

@main chasm

hallow beaconBOT
#
nelfye

To be able to use the code I need to understand it

orchid kernel
orchid kernel
# hallow beacon
it imports the required modules for the code to work, then with the system.runInterval it runs the code in the ()=>{//code} at a constant interval, then in the code it setsup a variable list that is a array, then it setsup another variable scores which is a array scores and participiants in that objective, next it sorts the scores array to descending order(greatest to least) then it loops through all the elements in the scores array(for (let i = 0; i<scores.length; i++)) then pushs that data to the list array, then outside the loop, it setup a variable board that  joins the list array into a string that is seperated by the \n seperator(like when you press ctrl space in discord to make a new line) then it gets the entities in the world with the tag leaderboard then finds the entity that has the tag leaderboard and sets the the nameTag of the entity to the board variable, hope this explains it, if not then look at the docs
quick flame
#

make a leaderboard where? like on a entity or on a form?

main chasm
#

entity

#

y try

main chasm
quick flame
#

what entity

main chasm
#

any

quick flame
#
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§4${i + 1}. §c${name}§r: §p${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);```
main chasm
#

o

quick flame
#

replace entity:hologram

#

with whatever u want

main chasm
quick flame
#

ok

hallow beaconBOT
#
nelfye

or I choose the score

#
nelfye

or I choose the score

main chasm
#

is good

orchid kernel
#

what he gave

#

what about what I gave?

main chasm
#

i try to make it good

buoyant nimbus
#

what tags do i need to give to the entity

quick flame
#

uhhh

#

score and title

buoyant nimbus
#

i don't understand the code

#

sorry

#

i already have the scoreboard

quick flame
#

/tag @s add "title:§6Kills"

buoyant nimbus
#

i just need the commands to display the text on the entity's name

quick flame
#

/tag @s add "scores:Kills"

#

and replace @s with entity

buoyant nimbus
#

ok

quick flame
#

did it work?

buoyant nimbus
#

didn't work

quick flame
#

what did u do

buoyant nimbus
#

i changed the entity to a cow

#

minecraft:cow

#

then

#

i used these commands on the cow

quick flame
#

o wait

#

i forgot

#

"objective:Kills"

buoyant nimbus
#

oooh ok

clever tapir
#

One question, does this code work?

buoyant nimbus
#

@quick flame didn't work also

#

it's not showing errors on the console

quick flame
#

uhhh

#

yes code works

clever tapir
#

I'm going to try the code

quick flame
#

uhh

#

wait what commands did u do exactly

clever tapir
#

where to put the tag

#

and the scoreboard

quick flame
clever tapir
#

It doesn't work when I put the objective tag it gives an error

buoyant nimbus
#

and i added an objective with the name "Kills"

quick flame
#

hm

#

u got everything updated?

buoyant nimbus
#

no errors on the console

quick flame
#

lemme join u