Minecraft – Can you use /testfor to detect the death of a player

minecraft-commandsminecraft-java-edition

Can you use the /testfor command in minecraft 1.8 to find out if a player has died ?

I need it for my map, where 2 players (Only) are pitted against eachother. I want it so that when one of them dies, it will detect the death and declare the victor.

Best Answer

The usual way to test for dead players is to set up a scoreboard objective. Check out the linked wiki page for general information, as well as the command reference for using scoreboards.

First, you have to set up an objective, I'll call it hasDied using

/scoreboard objectives add hasDied deathCount

Using the deathCount criterion means that it is automatically updated when someone dies. Now you can check whether a player has died using

/testfor @a[score_hasDied_min=1]

On a clock. The output of this command block will turn on once at least one player, somewhere, has a death score of at least 1. As part of this output (or the game start routine) you should reset the scoreboard objective for everyone using

/scoreboard players set @a hasDied 0

For an unknown number of players detecting the last man standing can be tricky, but it's doable. You can easily track people that have not died (using @a[score_hasDied=0]), and detect when the comparator outputs at signal strength 1.