Minecraft – Output power if X players in radius Y around the command block

minecraft-commandsminecraft-java-edition

I'm new to Command Blocks in Minecraft and want to solve this problem:

If X players in radius Y around the command block, then output power.

I read a bit about testfor and tried things like testfor @p[r=5,c=2] which I hoped would only output power if there are at least 2 players in a radius of 5. Doesn't work. Then I only tried testfor @p[r=5] and added a comparator block at the end and hoped it would output as much power players are in the given radius. Doesn't work.

How can I actually test how many players are in a specific radius? How do I only output power if a given number of players is passed?

Best Answer

@p targets the closest player so will only find 1 player each time, even if more are within the radius you have given.

@a targets all players with the arguments given, so should work for either of the methods you are trying.