How to get player with highest, second highest, etc. score?

I'm trying to make an event for a server. The goal is simple, you have to do as many tasks as possible before the timer runs out. I already figured the timer out.

However, here's the catch. I want to teleport somewhere else the 3 players that have the highest score, the second highest and the third highest. I have a different room for each of them.

I tried at first to teleport the player somewhere if they had completed 20 tasks, somewhere else for 15 and 10. But there may be 3 players that have done 10, 11 and 12 tasks, in that case they would all go into the third room, but I need them to get teleported to different places.


If you want to keep the numbers, first copy the scores to a temporary scoreboard, because some will get removed during the calculation. This command copies everyone's score in the scoreboard tasks to the scoreboard temp:

/execute as @a run scoreboard players operation @s temp = @s tasks

Then you give a dummy player the lowest possible value in that scoreboard. A name starting with $ cannot occur as a real player name.

/scoreboard players set $highest temp -2147483648

Here comes the magic: scoreboard players operation […] > assigns the higher of two values to the left side, in this case $highest's score in temp. By comparing every player's score with $highest this way, you end up with the highest value, because it increases the score of $highest every time it encounters a higher value in any of the players' scores.

/execute as @a run scoreboard players operation $highest temp > @s temp

Now you can do something with a player that has the same score as $highest in that scoreboard:

/execute as @a if score @s temp = $highest temp run tp @s <coordinates>

To get the second player, the third and so on, you just need to repeat that process without the player(s) with the highest value:

/execute as @a if score @s temp = $highest temp run scoreboard players reset @s temp

Now use commands 2, 3 and 4 again, with a new teleport target.