I want to run a command when there are only 10 entities left. I know you can use /execute unless entity to detect when all matching mobs have died, but it doesn't work if you want to run a command when there are for example 10 entities left.

A command like /execute if entity @e[limit=10] does not work, it also triggers if there is only one entity.

Such a counting system could also be used to determine once there is only one player left in an arena, meaning they have won a battle or similar.

  • Hello! We can help you with issues that you come across while you're building your command, but it is much more difficult for us to come up with very specific commands and do everything for you. Can you show us what you have tried?
    – 54D
    Commented Mar 28, 2020 at 3:55
  • Hi! I found EIGHT other questions on gamingSE that all ask essentially this or variantions of it. This one is the only one with an answer that is not outdated for at least two years, so I'll make this question a central place to direct all the other ones to (and also in the future). To make it more suitable for that, I'll reword a bunch of it, please check the edit history (or the notification) and see if you're OK with the edits. Commented Apr 20, 2020 at 4:00

2 Answers 2


Create a dummy scoreboard objective (I've called it entityCount here):

/scoreboard objectives add entityCount dummy

Then, count all the entities you want by getting each entity to increase a score by 1. First command is repeating, others are chain.

/scoreboard players set count entityCount 0
/execute as @e[type=zombie] run scoreboard players add count entityCount 1
/execute as @e[type=skeleton] run scoreboard players add count entityCount 1
/execute as @e[type=creeper] run scoreboard players add count entityCount 1

Then run your command when the score matches 10:

/execute if score count entityCount matches 10 run [...]

If you want to detect "at least 10", use this:

/execute if score count entityCount matches 10.. run [...]

If you want to detect "at most 10" (including 0), use this:

/execute if score count entityCount matches ..10 run [...]
  • 1
    We used this to check if we lit every cave around a slime spawner. Just execute /scoreboard objectives setdisplay sidebar entityCount in console to see the count live
    – narrowtux
    Commented Mar 18, 2021 at 20:38

The reason why /execute if entity @e[limit=10] does not work for this is that you're essentially telling the game this: "Make a list of all entities and limit that list to 10 entities. Then do something if there is something on that list." Limiting the list does not help you in figuring out whether there are more or less than 10 entities.

AMJ's answer is a method of counting entities that worked for a long time already (updated to 1.13+ command syntax), but since 1.13 you can also do it with better performance by using the length of Minecraft's internal entity lists instead of iterating your command over each of them.

You need a scoreboard objective for this as well:

/scoreboard objectives add entities dummy

Now you can efficiently count all entities with this slightly weird looking command:

/execute store result score @s entities if entity @e

You actually do not need run and a following command here. This is one of the few use cases of the fact that the return value of /execute if behaves similar to the old /testfor command in 1.12 and before: It returns the number of matching entities, which you can then store in a scoreboard using /execute store.

You can then use the score like in AMJ's answer, for example doing something if there between 10 and 15 entities is done like this:

/execute if score @s entities matches 10..15 run …

This method is very efficient, because Minecraft already has a list of all entities and executing this command causes it to take a shortcut: Instead of iterating over all entities and adding 1 every time, it just gives back the length of that list, which is already in memory. This will be very noticeable if you use such a command for large amounts of entities or a lot of times, like in looping functions.
Of course you'll want to limit the selector @e somehow when you actually use this command. If you for example use @e[type=zombie], Minecraft does not go through all entities in the world and checks each for being a zombie, but also takes a shortcut, because for efficiency, Minecraft stores a list of entities per type, so in this case it can just return the length of the list of all zombies.
Because these lists per entity type exist, it is actually more efficient to write @e[type=armor_stand,tag=foo] instead of just @e[tag=foo], even if you know for sure that nothing else ever gets tagged with foo, because Minecraft only needs to check all armour stands for the tag and not all entities.

Source for efficiency explanation: https://minecraftcommands.github.io/commanders-handbook/selector-argument-order (archive)

  • This needs updating. The original link is broken (luckily not the archive) and the type filtering explanation is not fully correct. Commented Jun 26, 2022 at 10:50

You must log in to answer this question.