Minecraft problem with commands – /testfor health

minecraft-commandsminecraft-java-edition

I have a problem with my redstone/command blocks circuit.
My redstone circuit:

redstone circuit

In command block 1, the command in that block is:

/testfor @a [score_health=19,score_health_min=1]

(this sends a redstone signal when their is a player with less than 10 hearts).

In command block 2 the command is:

/replaceitem entity @p slot.armor.head diamond_helmet 1 0

(when activated it places a diamond helmet on your head)
The rest of the commands are for other armor pieces.

When someone has less than 10 hearts, it activates the other commands but gives the nearest player the armor.
How do I make it so that the "armor wear" commands only effect the player who has less than 10 hearts?

Best Answer

/replaceitem has a target selector. Just use /replaceitem instead of /testfor.

/replaceitem entity @a[score_health=19,score_health_min=1] slot.armor.head minecraft:diamond_helmet 1 0

Relying on physical redstone logic when dealing with multi-target situations will make it difficult or impossible to support it. For example, you mechanism appears to turn on and stay on as long as a player has that health, which means any future players that meet those requirements will not be affected because the mechanism is locked due to the first detection. Removing physical logic and using /replaceitem on the clock will allow it to support multiplayer.