6

Can you use the /testfor command in Minecraft to detect if a player has died?

I need it for my map, where two players are pitted against each other. I want it so that when one of them dies, it will detect the death and declare the victor.

3 Answers 3

14

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.

1
  • 1
    1.13 syntax for second command: /execute if entity @a[scores={hasDied=1..}]
    – pppery
    Jul 9, 2019 at 4:30
0

Just put this in a command block on a clock (if you wanna know a super fast clock just ask me) and put this command inside:

/testfor (player name here or @a(all players) @p (nearest player) @e(nearest entity) (also you can add this to the end of @r or @e:[type=(entity name here (see all by typing /summon then pressing Tab))]

then have a comparator coming off of that going into a block with a torch on it the connected to the torch would be your output.

1
  • 1.13 syntax: /execute if entity ...
    – pppery
    Jul 9, 2019 at 4:31
0

You have to add a scoreboard. In this case, we have to add the scoreboard deathCount . You can name it whatever you like--for this example i'll name it deaths. Here's the command to add the scoreboard: /scoreboard objectives add deaths deathCount, then, in a separate command block, put: /scoreboard players set @a deaths 0. To testfor player's death, put in another command block: /testfor @a[score_deaths_min=<number of deaths>]. And there you go!

P.S. what is the name of your map, i'm gonna try to play it with my friend?

1
  • 1.13 syntax for last command: /execute if entity @a[scores={deaths=<number_of_deaths..>}]
    – pppery
    Jul 9, 2019 at 4:31

You must log in to answer this question.

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