Minecraft – How to teleport only people within a specific area

minecraft-commandsminecraft-java-edition

Is there a way I can set an area, bigger than one block, to teleport players from? I'd like to randomly teleport people that are within this area to two or more destinations.

I'm making a minigame in which players all start in a hub, and a command blocks picks one of the players in the hub area randomly and teleports them to either the blue team or red team side. My problem when trying this was that it would teleport people no longer in the hub area, making it so that a person on the blue team could randomly get swapped to the red team or from red to blue.

How can I make the command only target people in a certain area?

I'm currently on Minecraft 1.10.2 for PC.

Best Answer

If I understood well, you want the /spreadplayers command, which teleports players to random places inside a specified area in a command:

/spreadplayers <x> <z> <spreadDistance> <maxRange> <respectTeams> <player> 

x, z: The middle of the area you want to teleport players (press F3 to see coordinates)

spreadDistacnce: The minimum distance between teleported players

maxRange: The maximum teleport distance from the x and z coordinates

respectTeams: True or false. When true, targets on the same team will be teleported to the same location.

player: The selector of what you want to teleport, eg. @p

Example:

/spreadplayers 0 0 1 10 false @a

This will teleport all players to random locations, but at least 1 block away, from x = -10 and z = -10 to x = 10 and z = 10.

Example 2 (requested in the comments):

/spreadplayers 100 100 0 5 false @r[x=0,y=60,z=0,r=10]

This command will teleport a random player that was less then 10 blocks away from XYZ 0, 60, 0 to a random location within 5 blocks of X, Z 100, 100.

For more information about the /spreadplayers command, visit the Minecraft Wiki or the Minecraft Forum