0

I am trying to detect if exactly 1 player is in a radius using testfor. To my understanding, I should be able to use the c argument to count players, but I am having no such luck.

/testfor @e[x=0,y=0,z=0,r=100,c=1]

The intended output of this command is to return true if exactly 1 players is within 100 blocks of 0,0,0, and false if not.

If it makes any difference, testing for less than 2 players would solve my problem as well.

0

1 Answer 1

0

c=X will truncate the list down to X players. For example, if @a finds Alice, Bob and Carol, then @a[c=2] will find Alice and Bob.

The selector will still be successful so long as it finds at least one player, even though there were more players. This is similar to how other selector arguments such as x=0,y=0,z=0,r=100 only need one player to match to be successful, not every player on the server.


To determine if there is exactly one player within a certain radius, you can use a command blocks' SuccessCount, which with /testfor will store how many players were detected. You can thus test when this is exactly one with a second command block, and have the conditions running off of that:

Setup

Commands:

1. /testfor @a[x=0,y=0,z=0,r=100]
2. /testforblock ~1 ~ ~ command_block * {SuccessCount:1}
3. commands you want to activate when exactly one player is within the radius

Keep in mind that you'll need to change command_block to match the type of command block the one with testfor in is (e.g: chain_command_block or repeating_command block), and ~1 ~ ~ to match where it is.

8
  • In the command block 2, I have the command testforblock 4 64 -15 minecraft:repeating_command_block * {SuccessCount:1}. This returns false even when command block 1 has found exactly one player. Command block #1:` testfor @e[x=0,y=0,z=0,r=1000]`. I have verified that the coordinates are correct, as well as the type of command block. Commented Dec 24, 2016 at 17:26
  • It appears that the first command block is ALWAYS returning 0 as the SuccessCount Commented Dec 24, 2016 at 17:28
  • @JulianLachniet What's the output message of the first command block?
    – SirBenet
    Commented Dec 24, 2016 at 17:33
  • Found ilovescratch Commented Dec 24, 2016 at 17:34
  • I would have recommended using dummy scoreboard objectives that are applied, tested, then removed <b>in that order</b> ... but as I have yet to understand how to do so, I am at a loss of creating an answer. One thing is certain with my idea: I do know it would involve a few more command block checks for "less than" and/or "greater than" the target number of players if the result needs it to be existing. Commented Dec 24, 2016 at 17:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.