How to target player with the lowest health? [duplicate]
Quijibo has a good idea, and I would like to take this opportunity to expand it into an official answer.
First, you need a free tag name that will be applied to the player to target. I'll be using lowestHealth
as my tag. The tag will be applied to the player with lowest health, using the nearest player if there's a tie.
Place down an impulse command chain consisting of 20 command blocks.
Insert the following commands into each command block.
-
Remove the tag from all players.
/tag @a remove lowestHealth
-
At your position, add the tag to the nearest player with health 0.
/execute as YOUR_USERNAME at @s run tag @p[scores={health=0},name=!YOUR_USERNAME] lowestHealth
-
If the tag has not yet been applied, target the nearest player with health 1.
/execute unless entity @a[tag=lowestHealth] as YOUR_USERNAME at @s run tag @p[scores={health=1},name=!YOUR_USERNAME] lowestHealth
-
If the tag has not yet been applied, target the nearest player with health 2.
/execute unless entity @a[tag=lowestHealth] as YOUR_USERNAME at @s run tag @p[scores={health=2},name=!YOUR_USERNAME] lowestHealth
-
If the tag has not yet been applied, target the nearest player with health 3.
/execute unless entity @a[tag=lowestHealth] as YOUR_USERNAME at @s run tag @p[scores={health=3},name=!YOUR_USERNAME] lowestHealth
Continue with commands up to health 20.
-
Run whatever command you want with the tagged person. For example...
/tellraw @p [{"text":"You have the lowest health! Be careful..."}]