I'm trying to create a command block which adds players to the team "spawn" if they are within 2 blocks.

I can get it to add players to spawn, but not remove or change team when they go outside of the range?

Once they leave the spawn distance they are supposed to leave the team but this isn't working.

Can someone please explain why it isn't working, and offer a solution?

My commands are as follows:

execute if entity @a[distance=..2] run team join Spawn @s[distance=..2]
execute if entity @a[distance=2..] run team leave @s[distance=2..]

I even tried making another team for the players to join instead but this also doesn't work.

execute if entity @a[distance=3..] run team join notSpawn @s[distance=3..]
  • @s[distance=2..] means "myself, if I am at least 2 blocks away from myself". That can never work. You could simply remove the second [distance=2..], but MegaCrafter10's answer gives you a much better alternative. In general, I would recommend reading or watching a commands tutorial, you seem to have misunderstood some core concepts, like how /execute changes the command context. Commented Mar 5, 2020 at 15:04
  • Yeah i am a coder student but i seem to be quite trash when it comes to java based commands & development, thankyou for your assitance
    – Taum
    Commented Mar 5, 2020 at 17:13
  • Minecraft commands have nothing to do with Java. And generally it's better to not see Minecraft commands as a programming language, but more like a system that grew from simple teleporting, world modification, etc. into something bigger that can be used to write full programs, but isn't really intended for that. Commented Mar 5, 2020 at 18:12
  • Nothing to do with java? the syntax is extremely similar and it is built from java but ok dude :)
    – Taum
    Commented Mar 6, 2020 at 1:41
  • The syntax is not at all similar to Java. And sure, Minecraft runs with Java and executes commands, but the commands are very similar in MCBE and that game is written in C++. There's no relation between Java and why commands are the way they are. Commented Mar 6, 2020 at 6:13

2 Answers 2


The @s selector refers to the entity which is executing that command. Since the command is being executed by a command block, @s is referring to the command block and that's why it's not working.

There is a much easier way of doing this which doesn't require you to test for anything.

To add someone within range to a team: /team join someTeam @a[distance=..5]

And to remove players from the team if they are out of range: /team leave @a[team=someTeam,distance=5..]

  • Thanks dude, greatly apreciated
    – Taum
    Commented Mar 5, 2020 at 17:21
  • @Taum: can you please click on the check mark on the left side of the answer, to set the question to solved, if the answer answers your question? Commented Mar 7, 2020 at 12:29

If players get in the area:

execute positioned as x y z run team join Spawn @a[distance=..2]

If players get outside:

execute positioned as x y z run team join notSpawn @a[distance=2..]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .