/testfor command to count players? [duplicate]

How can I make a /testfor command emit a signal when there is only one player in the required area? I understand /testfor @a[x=,y=,z=,r=].


Solution 1:

A quick and easy way is to check the SuccessCount value of the command block using /testforblock:

Repeating > Chain > Conditional Chain

  1. Run the /testfor command. The c parameter is set to 2 to prevent unnecessary processing, as /testfor is processed equal to the number of targets obtained. We only need to know if there's 1 or "more".

    /testfor @a[x=10,y=64,z=10,r=3,c=2]
    
  2. Run a /testforblock command that is targeting command block #1 and looking for a SuccessCount of 1.

    /testforblock X Y Z minecraft:repeating_command_block -1 {SuccessCount:1}
    
  3. Conditional. Command to run if there was only one player in the area.

    /say Only 1 player in area.
    

Checking for a SuccessCount of 2 means there were more than 1 player, while a value of 0 means there were no players at all.