Minecraft – How to find the name of a player(s) within a certain area

minecraft-commandsminecraft-java-edition

I am trying to make a PvP mini-game and I'm trying to make it so that when a certain amount of players are left, it awards the players inside of the arena.

I have already managed to make the player counter for the area using some command blocks and a comparator, but I only know how to find the amount of players with testfor, not the actual names of the players.

How can I find the names of the players inside the arena?

Best Answer

Power a repeat command block with this command:

/testfor @a[x=X,y=Y,z=Z,r=R]

Where X, Y, Z are the middle coordinates of the arena, R is the radius of the arena. Now, put this command in a repeating command block:

/testforblock X2 Y2 Z2 minecraft:command_block -1 {SuccessCount:SC}

Where X2, Y2, Z2 are the coordinates of the 1st command block and SC is the amount of players you want to be left to award them.

Next to the 2nd command block place a chain command block with this command (or another if you don't want to give items, but be sure to use the @a[x=X,y=Y,z=Z,r=R]):

/give @a[x=X,y=Y,z=Z,r=R] minecraft:THE_AWARD_YOU_WANT

Where X, Y, Z are the middle coordinates of the arena.