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.
Arqade is a question and answer site for passionate videogamers on all platforms. It only takes a minute to sign up.Sign up to join this community
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
/scoreboard objectives add hasDied deathCount
deathCount criterion means that it is automatically updated when someone dies. Now you can check whether a player has died using
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.
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.
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?