Say I want to have a command that applies to everyone on a server except 2 people. Is there a target selector for that?
3 Answers
In order to do an all-but target selector, you need to add an exclamation point (!) before the name of the thing excluded from the command.
For example, the command
/testfor @a[name=!IfThenElse]
would return true if there is a player other than the player with the name IfThenElse online when the command is executed. In order to modify this command, simply replace the name IfThenElse with a different username.
With the all-but you can make an effective entity clearer:
/kill @e[type=!Player]
This would kill all entities (minecarts, mobs, dropped items) except for players.
However, since you can only select one all-but name, the scoreboard must be used.
Start by using the command
/scoreboard objectives add testnot dummy
and setting testnot to 1 on players you don't want to be effected by whatever command:
/scoreboard players set IfThenElse testnot 1
Replace IfThenElse with a username. Then you can run a command with the target selector
@a[score_testnot=0]
, which would effect everyone but the players who had their testnot score set to 1.
I'm going to be surprised if I got the command syntax right. Edit as needed.
Normal target selectors cannot select 2 specific people, the solution is to use a scoreboard to mark the players.
Setup:
/scoreboard objectives add Selector dummy
Clock:
/scoreboard players set @a[name=<Name of Target>] Selector 1 //One of these commands per person
/scoreboard players set @a[score_Selector_min!=1] Selector 0
Selector:
@a[score_Selector=0] to select non selected
-
Do players automatically get added to the Selector scoreboard as having a score of 0? Commented Aug 24, 2015 at 4:56
-
-
So why not just use
@a[score_Selector_min!=1]
in the first place? Also, what's_min
do? Commented Aug 24, 2015 at 14:02 -
_min means the minimum amount of score they need, and well i guess you can do it with a _min=!1– ModdlCommented Aug 24, 2015 at 20:01
-
So
@a[score_someVariable_min=1]
is basically how to do@a[score_someVariable>=1]
, right? Commented Aug 25, 2015 at 2:16
So from what people have said and some of my own research here's the easiest way to do this:
Set up a scoreboard, called ExcludeSelection for example:
/scoreboard objectives add ExcludeSelection dummy
Set the players you want to exclude to 1 in this scoreboard:
/scoreboard players set <username> ExcludeSelection 1
(do this for every player you want to exclude)Add 0 to all player's scores (basically this adds players not on the scoreboard to the scoreboard without messing up existing values):
scoreboard players add @a 0
(in a loop, perhaps a setblock clock)And then to select all players except the ones you want to exclude, just use:
@a[score_ExcludeSelection=0]
I personally am using this to prevent anyone except the co-owners of the server (moi and my friend) from using creative mode. Just because I feel like it, here's what I think I'm going to do in addition to the first four steps.
In a setblock clock, I'll put tell @a[score_ExcludeSelection=0,m=1] You are not allowed to use creative mode.
in one command block, and in another, gamemode @a[score_ExcludeSelection=0,m=1] survival
.