Minecraft – Testing if there is an entity with a objective score lower than another objective score

minecraft-commandsminecraft-java-edition

Say there is a player called Dave with a Health score of 10 and a MaxHealth score of 20. I need to check if there is any players with a health score lower than MaxHealth and tellraw it. Also is there a way to add values onto Health say 60 or 20 but without it going over MaxHealth? E.G. Health + 60 = MaxHealth (without going over)? Thanks in advance.

Best Answer

How can I tellraw all players with less than full health:

You can use

/scoreboard players operation <targetName> <targetObjective> <operation> <selector> <objective> 

to modify a players score by the value of another score(even between players).

But unfortunately, I couldn't find an objective criteria for MAX_HEALTH. (might be worth a feature request on bugs.mojang.com)

If there should be one, you would need 3 objectives to do what you want:

/scoreboard objectives add Health health
/scoreboard objectives add MaxHealth maxHealth
/scoreboard objectives add HealthLost dummy

You need a clock to run all of these following commands:

execute @a ~ ~ ~ /scoreboard players operation @p HealthLost = @p MaxHealth
execute @a ~ ~ ~ /scoreboard players operation @p HealthLost -= @p Health

For the tellraw, use the selector tag in the json text:

/tellraw @a {text: "All these players are hurt: ",extra:[{selector:"@a[score_HealthLost_min=1]"}]}

How can I heal players without going over maxHealth:

You can't modify a health scoreboard value. Instead use the

/effect <player> <effect> [seconds] [amplifier] [hideParticles]

command to heal players. The amount of health added is equal to duration * 2 ^ amplifier

See Potion effects (ID 6)

Edit according to comment:

(All objectives = dummy) You can use the operation command again to achieve this: Just add an arbitrary value to your score and then execute this:

/execute @a ~ ~ ~ /scoreboard players operation @p Health < @p MaxHealth

This will limit the "Health" score of every player to his "MaxHealth" score