How can I make players share damage using command blocks?

How can I make players share damage using command blocks? - Mother with daughter looking at laptop

I'm making an adventure map where I want players to share damage - when player 1 takes some amount of damage, player 2 will also get damaged that same amount. I used scoreboards with health, did a scoreboard to test for player's health and I know how to damage using potion effects, but I don't know how to testfor and deal damage to one specific player (the player with higher health).

Could I get some advice on this?



Best Answer

You should use a dummy variable and target selector arguments.

Run once

Have a dummy score, call it healthDif.

/scoreboard objectives add healthDif dummy

Run every tick, in order

Set each players healthDif score to their own health score:

/execute @a ~ ~ ~ scoreboard players operation @a[c=1] healthDif = @a[c=1] health

Subtract the other player's healthDif score from your own:

/execute @a ~ ~ ~ scoreboard players operation @a[c=1] healthDif -= @a[rm=0] health

Now players with a positive healthDif score will have more health than the other player. Target with @a[score_healthDif=1]
Note this will only work for two players. In order for it to work with more, you have to take a few extra steps.


Run once

First you'll need a dummy variable to store miscellaneous variables. Call it vars. Note: If you already have a variable for this (you likely do), you can skip this step.

/scoreboard objectives add vars dummy

Run every tick, in order

Put these between the two commands from before.

First, set the #nplayers to 0:

/scoreboard players set #nplayers vars -1

Now, have each player increment #nplayers:

/execute @a ~ ~ ~ scoreboard players add #nplayers vars 1

Finally, multiply each players healthDif score by this number

/scoreboard players operation @a healthDif *= #nplayers vars

This way, you can have any amount of players, and the system automatically adapts!




Pictures about "How can I make players share damage using command blocks?"

How can I make players share damage using command blocks? - Tin vessels and metal bucket with milk placed near bike leaned on shabby rusty wall
How can I make players share damage using command blocks? - Young multiethnic business partners speaking about strategy of job while browsing netbook in contemporary workplace
How can I make players share damage using command blocks? - Cheerful young diverse female friends laughing while watching funny video on smartphone during coffee break in cozy cafe



How do you use a command block to ruin a player?

To damage a player, set that player's damage scoreboard to how much damage you want to apply(negatives heal players), then run the function editor:deal_damage as the player you want to damage and you are done.

How do you make a player take damage in Minecraft?

You cannot. There are various commands that are unavailable, probably due to the ability for abuse (such as reopping yourself after having your admin privileges revoked).

Can you op people with command blocks?

The damage command is a brand new command in Minecraft: Bedrock Edition. This command allows users to apply a specific type of damage to a specified entity from a specified source. For example, dealing fire damage to a creeper or fall damage to a zombie.



Custom Damage Amounts in Minecraft




Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: Tatiana Syrikova, Plato Terentev, Alexander Suhorucov, Ketut Subiyanto