detect if a mob hit a player in minecraft with scoreboard in a datapack
To detect if a mob has hit a player in Minecraft using a scoreboard in a datapack, you can follow these steps:
-
Create a new scoreboard objective:
/scoreboard objectives add hitPlayer minecraft.custom:minecraft.mob_kills -
Create a function file in your datapack to detect the hits. For example, create a file called
detect_hits.mcfunctionin thedata/<your_datapack_name>/functionsdirectory. -
In the
detect_hits.mcfunctionfile, you can use theexecutecommand to detect if a player has been hit by a mob. For example, to detect if a player named "Player1" has been hit by any mob:execute as @a[name=Player1] at @s run execute as @e[type=!player,distance=..0.5] at @s if entity @s[distance=..0.5] run scoreboard players add @s hitPlayer 1This command will add 1 point to the "hitPlayer" scoreboard objective for the player named "Player1" whenever they are hit by any mob within a distance of 0.5 blocks.
-
Save the
detect_hits.mcfunctionfile. -
In your datapack's
pack.mcmetafile, make sure you have the correct load order for the functions. For example:{ "pack": { "pack_format": 7, "description": "Your datapack description" }, "functions": [ "<your_datapack_name>:detect_hits" ] } -
Load or reload your datapack in the game:
/reload
Now, whenever the player named "Player1" gets hit by any mob, their "hitPlayer" score will be increased by 1. You can then use this score for further processing or to trigger other events in your datapack
原文地址: https://www.cveoy.top/t/topic/h6jU 著作权归作者所有。请勿转载和采集!