How do I create a random team chooser for a Minecraft server?
For example: When you step on a block, you are automatically assigned to a random team: transported to that team's side of the building, given team-coloured armour etc.
Arqade is a question and answer site for passionate videogamers on all platforms. It only takes a minute to sign up.Sign up to join this community
Note: This is a solution that needs a small amount of commands and is easy to explain. You can do a variety of optimisations, but this is the basic strategy.
This task is easiest with a standard randomiser. For that, firstly put some regular command blocks in a row, each with an armor stand above it, like this:
The rotation of the command blocks and the armor stands doesn't matter. The armor stands can be invisible, marker, NoAI, NoGravity, small, whatever, they just have to be there.
Put the commands into the command blocks of which you want to randomly select one:
/team join <teamName> @a[x=_,y=_,z=_,dx=1,dy=1,dz=1]
<teamName> with the different team names in each command block. Replace
_ in all of them with the coordinates of the block where the players should stand in (the coordinates of the pressure plate for example).
The command to execute a random one of these command blocks is then:
/execute at @e[type=armor_stand,sort=random,limit=1] run setblock ~ ~ ~ redstone_block
Then, to reset it again:
/execute at @e[type=armor_stand] run setblock ~ ~ ~ air
You'll probably want to restrict the selector for the armor stands in some way, by location, name, tag or similar.
You can for example set these two commands up by putting a pressure plate down that the players should stand on, under that the ground, under that a regular command block pointing down or to the side and where that's pointing a chain command block. This could for example look like this:
Note: This procedure does not guarantee equally sized teams. In theory everyone could get assigned to the same team (but that's unlikely).
So the easiest way to do this is to capture players at spawn with a contraption like this:
This is a large area (the whole of the spawn chunk) laced with tripwires. When a new player joins, they land here and it triggers command blocks. These command blocks:
You can do this with a pressure pad instead, but this way makes it easier to automate, so you don't have people floating around who aren't in teams (unless you want this).
What you would need to do is combine this with a rapid pulsar - essentially a very fast ticking clock that is switching between two different sets of command blocks (or however many sets you want). Depending on which set of command blocks is enabled when the player spawns, they will be assigned a different team.
Other options include simply alternating - which makes sure that the teams are balanced. This would work by simply using a T flip-flop to switch the command block banks each time.
There are two ideas I have, I'm not sure which one would be ideal for you.
The first idea: if you really want modern command blocking and a super, super clean experience, you could do something that isn't really random, but could work. First of all, you'd set up a custom scoreboard objective and teams:
/scoreboard objectives add teamrng dummy teamrng /scoreboard teams add red /scoreboard teams add blue /scoreboard teams option red color dark_red /scoreboard teams option blue color blue
Then, somewhere away from the lobby, you'd place down a sponge block (or any other block you won't use for building, it doesn't really matter) and have people needing to get a team teleport on top of the sponge. Place the following command blocks in a loop, i.e. one tick one activates, the next tick the other activates, etc, each command block with one chain unconditional command block branching out from it:
first command block:
/execute @a ~ ~ ~ detect ~ ~-1 ~ sponge 0 /scoreboard teams join red @s chain command branched from first: /execute @a[team=red] ~ ~ ~ detect ~ ~-1 ~ sponge 0 /tp @s redx redy redz
second command block:
/execute @a ~ ~ ~ detect ~ ~-1 ~ sponge 0 /scoreboard teams join blue @s
chain command branched from second:
/execute @a[team=blue] ~ ~ ~ detect ~ ~-1 ~ sponge 0 /tp @s bluex bluey bluez
This should be sufficient. I haven't had access to Minecraft in a few hours, I might be messing up some formats, but it should work.
The second idea is much more difficult, less clean, but truly random, let me know if you need this.