Minecraft /execute as a player if they are alive

minecraft-commandsminecraft-java-edition

I have an /execute command in a repeating command block, such as:

execute @a ~ ~ ~ say hi

This makes every player say hi every tick, even if they are dead and at the 'Respawn?' screen.
How would I make the /execute only happen if the player is alive?

Best Answer

This can be done with the health scoreboard. Create an objective with:

/scoreboard objectives create h health

This creates an objective h, which tracks player's health (by number of half-hearts).
Change your command to:

execute @a[score_h_min=1] ~ ~ ~ say hi

This only triggers if the player has at least a half-heart (they're alive).