How to find someone's personal best? (fastest time in a race?)
I am currently making a minigame to play right now, (specifically a racing game) but I am unsure how to find the personal best of a player at the end of the game. I have a timer setup, but my objectives seem to make this task a little difficult? I know the scores of the times will play into this, so I will explain what I have setup with the times.
1.) I have objectives, dispsecond and dispminute. These stand for Display Second and Display Minute. Simply, it functions as a normal timer. At the end of race, everyones' timers halt. So at the end of the race, one's score may be: 1-dispminute 39-dispsecond
Now my question still stands, how do I find the personal best?
Use scoreboard operations
I assume you also have the time stored in a non-formatted scoreboard, e.g. in ticks. Let's call that objective time
. If not, see below.
Make another dummy scoreboard objective called best
, and initalize it to 0, e.g. by running
/scoreboard players add @a best 0
When a player finishes the race, mark him in some way, e.g. by assigning a tag (I'll use tag=finished
). Then simply run
/execute @a[tag=finished] ~ ~ ~ scoreboard players operation @a[c=1] best < @a[c=1] time
The <
operator will assign the lesser score of best
and time
to best
. The execute
makes sure that both targets of the scoreboard command are the same player.
You can turn best
into bestminute
and bestsecond
the same way you converted time
into the display stats.
Converting times to a single number
If you don't have time stored as a single continuously increasing number, you can use scoreboard players operation
commands to turn your dispminute
and dispseconds
scores into one. Set up the new objective, and store the number 60 in a fake player (called #SIXTY):
/scoreboard objectives add time dummy
/scoreboard players set #SIXTY time 60
Now, set up a repeat/chain line and run.
/scoreboard players set @a time 0
/execute @a ~ ~ ~ /scoreboard players operation @a[c=1] time += @a[c=1] dispminutes
/execute @a ~ ~ ~ /scoreboard players operation @a[c=1] time *= #SIXTY time
/execute @a ~ ~ ~ /scoreboard players operation @a[c=1] time += @a[c=1] dispsecond
This will first reset the time score for every player. Then, the players' scores in dispminutes
will be added to time
, and multiplied by 60. Lastly, dispseconds
is added to time.