I want to execute a command on all players withhin a distance from an armorstand (Let's call him "A"). The distance is stored in the scoreboard "Distance" for that armorstand ("Distance" of A is e.g. 13). This means that execute as @e[name=A] at @s as @a[distance=..13] ... doesn't do the trick, because the 13 is a constant here and I don't really want a command for every possible distance from 1 to 100.

I had the idea to store the distance of all players to that armorstand in their "Distance"-score and then compare the 2 scores for every player. But I don't know a way to store the distance in an scoreboard or to get it with a command.

Is there a way to do this?

  • What do you mean by "doesn't do the trick....from 1 to 100"? Also, by "comparing distance, do you want to find the nearest player? – fasterthanlight Sep 9 '20 at 11:53
  • @fasterthanlight I would need one command for every possible distance, which I don't want if avoidable. But I also want to be able to change the distance for the armorstand without changing the code. – IceFreez3r Sep 9 '20 at 12:27
  • Have you tried taking a scoreboard variable and tweaking it with seperate commands, and taking that value as the max distance? – fasterthanlight Sep 9 '20 at 12:57
  • @fasterthanlight This pretty much exactly the question. How do I take that value as the max distance? – IceFreez3r Sep 9 '20 at 13:11
  • Ok, so you want nothing else but that, right? – fasterthanlight Sep 9 '20 at 13:12

I found a solution on YouTube:


Here is essentially what he is doing:

  • Take first entity with as @e[tag=a], Take the second entity with at @e[sort=nearest,limit=1]
  • Read and store the Position of both entities with
execute store result score @s math_in run data get entity @s Pos[0] 100
  • Compute the lengths x2-x1, y2-y1, z2-z1, square and add them
  • To compute the square root, he states to use the Newton-Raphson, 4 iterations approach. I don't have the math skills to quickly understand that method, but the results the datapack give are correct. To get the full list of commands, download his datapack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.