It want to test for the number of players in a gamemode.

I tried /testfor @a[m=0,c=1] but it only tests for the number of players, not the number of players in a gamemode.


5 Answers 5


If a selector such as @a[m=0] selects Alice, Bob, Carol, Dave, then @a[m=0,c=2] will select Alice, Bob. c=2 does not mean "only select anyone if there are exactly 2 players", but rather "limit the players selected to at most 2". Even if there are more than 2 players, the command will still select 2 players and succeed.

When a /testfor command is run, a command block's SuccessCount tag will be set equal to the number of entities that the selector found. You can then use a /testforblock command to check whether the first command block's SuccessCount is a certain number.

For example:

/testfor @a[m=0]
/testforblock X Y Z command_block -1 {SuccessCount:2}

Change X Y Z in the second command block to the coordinates of the first command block. You may also need to change command_block to chain_command_block or repeating_command_block.

The second command will only succeed if the first command has a SuccessCount of 2 (it found exactly 2 players). You can then run a conditional chain block off of that second command to activate whatever you want to happen when there are exactly 2 players:



So, a couple of things. First, the testfor command is pretty useless by itself. It isn't worth learning until you learn scoreboards and execute. A lot of command block contraptions are dependent on those two commands. Put testfor back on the shelf and learn other commands first. The only thing I find testfor useful for is testing that I'm using the correct selectors.

Which brings me to the second thing. You will only ever detect a single player in survivor mode. That's what the c=1 means. Remove that selector, and you should be good. Granted, there isn't much you can do with that command in a command block without also using something like stats (which pretty much necessitates using the scoreboard), since the only other option is to use analog redstone, which besides being effectively dead, is also severely limited.

  • So how would you advise I she make something that lets off a redstone signal when there is only 1 person in a radius left in survivel mode while the rest of the players are in spectator mode. Commented Mar 19, 2017 at 18:39
  • I would question why you're using redstone at all. You should try your best too remove all redstone from your command blocks. To answer your question though, it's a bit complicated. You might be able to do it with 3 or 4 command blocks, but I'd have to prototype it out first.
    – MBraedley
    Commented Mar 19, 2017 at 18:56

To check for how many findings your testfor found, you can use a second command block with a testforblock command. It's syntax, as described on the Minecraft Wiki is following ([] are optional entries):

testforblock <x> <y> <z> <block> [dataValue|state] [dataTag]

So to test if there is a command block at the coordinates X, Y, Z, the command is

testforblock X Y Z minecraft:command_block 0

where the minecraft: and 0 are optional.

Additionally, command blocks containing a testfor obtain a value called SuccessCount, which is the number of found entities. You can't extract this value though; you can only test if the command block has a certain SuccessCount:

testforblock X Y Z minecraft:command_block 0 {SuccessCount:S}

to check if the command block at X, Y, Z has a SuccessCount of exactly S.

The c=1 parameter in your command will trigger only to the first player in survival gamemode, so be sure to remove it.


1.13 Update

The m argument has been changed to gamemode, and the full name must be provided: [survival,creative,adventure,spectator]

The replacement for the /testfor command is /execute if entity:

execute if entity @e[gamemode=adventure]

You can get the count of entities by placing the store subcommand before the if subcommand:

execute store result score FakePlayer myObjective if entity @e[gamemode=survival]

You can test for the number by using /execute if score:

execute if score FakePlayer myObjective matches 0 run ...

I have another way using scoreboard first command block: /testfor @a[m=0,c=1] then a chain command block above it: /scoreboard players add @p Gms 1

then put this command in chat: /scoreboard objectives add Gms dummy

  • All that does is set a player's score and nothing else. A score simply existing doesn't do anything. That particular setup won't do anything anyways to answer the question.
    – Skylinerw
    Commented Mar 22, 2017 at 14:23
  • but he said the number of the players in a gamemode he can see from that score How many players are in gms Commented Mar 24, 2017 at 16:52
  • What your setup is doing is constantly adding 1 to the nearest-players score so long as there is a player in survival mode. It doesn't set a score based on the number of players in survival mode. It'll count up nonstop and does not represent the number of players in survival.
    – Skylinerw
    Commented Mar 24, 2017 at 20:01

Not the answer you're looking for? Browse other questions tagged .