TransWikia.com

How to tell if only one player is found by selector?

Arqade Asked on January 28, 2021

How can I tell if only one person is found by a selector?
For example, this would be useful to see if there is only one person with a playing tag, and you would know when everyone has died but one so they can win.

One Answer

Command

execute @a[tag=playing] ~ ~ ~ testfor @a[tag=playing,rm=0.0000000000001]

Explained

What we did was look for a player, then use that player to find another one of the same criteria, but with a rm argument which means the minimum distance (range) from the player we executed from. We put in a decimal with a bunch of spammed 0s and then a 1 to find someone other than the player we're executing from. Without this, the player we execute from would detect him/herself. This block returns true if 2 or more players have the tag playing.

Not Gate

We then take a comparator output from out command block and run it into a redstone torch, and above the torch have the command we want to run after. Follow Picture: Not Gate
When two players are found, the comparator will turn on and turn off the torch. When two players are not found, the comparator will turn off and the torch will turn on, activating the command block above the torch.

Work With rm Argument

This section only applies to the specific situations where an rm argument is already being used. If your selector that you wanted to use doesn't include rm, ignore this.
If you're already using the rm argument, we need a slightly modified approach. (For this example I will change the example selector from @a[tag=playing] to @a[tag=playing,rm=5].) Note you will have to put an armor stand on the command block and name it or tag it, I will name the armor stand testsel. So, instead of doing something like testfor@a[rm=5], we would do execute@e[type=armor_stand,name=testsel]~~~testfor@a[rm=5].

Command

From here we can change our previous command to:

execute @a[tag=playing,rm=5] ~ ~ ~ execute @a[tag=playing,rm=0.0000000001] ~ ~ ~ testfor @e[type=armor_stand,name=testsel,rm=5]

That command finds two people, outside the range of 5 and with the tag playing from the command block (@a[tag=playing,rm=5]).

Explained

That command first executes off of someone at last of 5 distance and the tag playing. Then, we find someone else with the tag playing. We then look and see if our armor stand that was on top of our block is at least 5 distance away.

Correct answer by Penguin on January 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP