# How can I calculate the distance to the closest player?

How do I calculate distance to the closest player and show it via a scoreboard on a vanilla server?

• Have you made any attempt to solve this yourself? Arqade works better when askers show effort to solve their own problems; we see that you have a problem you've worked on, and answerers respond to that. You also get a more specific answer that's tailored exactly to the part you're stuck, and Arqade gets a very specific question. Everybody wins! May 29, 2017 at 21:21
• @Frank yes, but there are many small problems (like how to get absolute value when I subtract coordinates, etc) seems you a bot btw May 29, 2017 at 21:30
• Do you need to do this with a vanilla server? It can be done easily on Bukkit.
– Roke
May 29, 2017 at 21:32
• I'd recommend adding that effort to your question; you'll get a better response. May 29, 2017 at 21:34
• @RookieTEC9 Yes. As I have restricted environment (Realms) May 29, 2017 at 21:35

Just in case anyone tried to look up how this problem could be accomplished, because this is the first result from google search. And I happened to work it out.

You can do this in 1.13+ now, with the `/execute store` command

The steps are as follows: store player coordinates > calculate the differences > square the differences > sum up > calculate the sqrt values. I'm using euclidean distance as the metric.

1. Store Player Coordinates:
You'd need to define objectives to store the values, I defined them as `"X", "Y", and "Z"`

``````execute as @a run execute store result score @s X run data get entity @s Pos[0]
execute as @a run execute store result score @s Y run data get entity @s Pos[1]
execute as @a run execute store result score @s Z run data get entity @s Pos[2]
``````

2. Calculate the Differences:
I defined another set of objectives to store the results `"X_difference", "Y_difference", "Z_difference"`, but you don't have to.

``````execute as @a run scoreboard players operation @s X_difference = @s X
execute at @a run scoreboard players operation @s X_difference -= @p[sort=nearest] X
execute as @a run scoreboard players operation @s Y_difference = @s Y
execute at @a run scoreboard players operation @s Y_difference -= @p[sort=nearest] Y
execute as @a run scoreboard players operation @s Z_difference = @s Z
execute at @a run scoreboard players operation @s Z_difference -= @p[sort=nearest] Z
``````
1. Square the Differences:
``````execute as @a run scoreboard players operation @s X_difference *= @s X_difference
execute as @a run scoreboard players operation @s Y_difference *= @s Y_difference
execute as @a run scoreboard players operation @s Z_difference *= @s Z_difference
``````
1. Sum Up:
I defined another objective `"distance"` to store the sum of the squared values from 3.
``````execute as @a run scoreboard players operation @s distance = @s X_difference
execute as @a run scoreboard players operation @s distance += @s Y_difference
execute as @a run scoreboard players operation @s distance += @s Z_difference
``````
1. Approximate the Square Root Values:
We can use a simple algorithm to calculate the square root values. You can find more info on how it works here. We need to create three new objectives: `sqrtI`, `sqrtX` and `realDistance`. I highly recommend using a datapack to implement this, as we need to recursively get closer to the real square.

Once you have your distance value, run the following to reset the sqrt algorithm:

``````# ... every tick, after calculating distance, do:
scoreboard players set @a sqrtI 1
scoreboard players set @a sqrtX 0
scoreboard players set @a realDistance 0
# then we start the algorithm by running another function:
execute as @a if score @s distance > @s sqrtX run function mydatapack:sqrt
``````

mydatapack:sqrt.mcfunction contains the following commands:

``````scoreboard players operation @s sqrtX += @s sqrtI
scoreboard players add @s realDistance 1
scoreboard players add @s sqrtI 2
execute as @s if score @s distance > @s sqrtX run function mydatapack:sqrt
``````

The `realDistance` objective then contains the real distance that every single player is away from you in blocks.

• On 1.18 I had to change all three `execute at @a run scoreboard players operation @s X_difference -= @p[sort=nearest] X` to `execute as @a at @s run scoreboard players operation @s X_difference -= @p[sort=nearest,distance=0.1..] X`
– Angs
Dec 19, 2021 at 12:35

You cannot show the distance closest to the player and show it via scoreboard due to the fact vanilla scoreboards are used for every player. This can be easily done with some bukkit code which will be both more efficient and cleaner to the eye.

• I can't install bukkit on Realms (( May 29, 2017 at 21:36
• For `each` player show distance to `nearest` (nothing impossible) May 29, 2017 at 21:39
• It is impossible! The scoreboards are not per player! Look this up yourself before you downvote the correct answer to your question.
– Roke
May 30, 2017 at 0:01
• @OwnageIsMagic I think the post says this is impossible because you can't show a player their number and nobody else's. It doesn't seem to address what commands should be used. May 30, 2017 at 18:09
• @ash4fun Ok. I missed something, but its still not impossible (youtube.com/watch?v=cThnjDTxUUQ) `PS` I didn't press any vote button on this stack yet May 31, 2017 at 20:49