#How do you run a command when a player is damaged from another player with a specific tag?
28 messages · Page 1 of 1 (latest)
Once you have finished, please close your thread.
Use damage sensors
@dense valley To go into further detail, the minecraft:damage_sensor component can be put in the player's behavior to detect taking damage. In this case we can use it to determine if the damager was a player who has that tag.
{
"components": {
"minecraft:damage_sensor": {
"triggers": {
"on_damage": {
"filters": {
"all_of": [
{
"test": "is_family",
"subject": "other",
"value": "player"
},
{
"test": "has_tag",
"subject": "other",
"value": "specific_tag"
}
]
},
"event": "on_damage",
"target": "self"
}
}
}
},
"events": {
"on_damage": {
// ...
}
}
}
From there, it depends on if you can use experimental features (Holiday Creator Features). If so, you can use the run_command event response to invoke a command directly:
"event": {
"on_damage": {
"run_command": {
"command": "say Ouch"
}
}
}
If not, then you would have to use an animation controller to detect if that event was ran. https://wiki.bedrock.dev/animation-controllers/entity-commands.html
Hello again. Your code works perfectly. Thank you very much. I would like now afterwards to add that the player doesn't take any damage.
@dense valley This can also be done within the damage sensor component. In the trigger object, add "deals_damage": false.
When I did added it, my code stopped working
This is what my code looks like:
"triggers": {
"on_damage": {
"filters": {
"all_of":[
{
"test": "is_family",
"subject": "other",
"value": "player"
},
{
"test": "has_tag",
"subject": "other",
"value": "specific_tag"
}
]
},
"event": "on_hunter_damage",
"target": "self"
},
"deal_damage": "false"
}
}
},
Make sure the property is named correctly: deals_damage, and not deal_damage
Oh, and there is a difference between the value false, which is a boolean, and the value "false", which is a string
Minecraft may not be reading the value correctly because you have quotation marks around it
Thank you very much!
Kind of topic, but do you know how to change the health of the player. I've previously used the attribute command in Java edition, but that isn't available in bedrock it seems
You can use component groups with the minecraft:health component to modify the max health. Unfortunately you would need hardcode the values you want, as they cannot be set dynamically
So I can't change it depending on for example an entities score from a scoreboard
If you hardcoded all of the values of that scoreboard, you could ... but no, setting the max health directly to the score's value is not possible
Hardcoding it works fine for me. Would I do that using some kind of IF-statement?
Yes, in whatever medium you are using to trigger those events to set the player's health. In commands that may look like this:
execute if score @s max_hp matches 1 run event entity @s hp_max_1
execute if score @s max_hp matches 2 run event entity @s hp_max_2
execute if score @s max_hp matches 3 run event entity @s hp_max_3
# ...
How are the hp_max (i.e 'hp_max_1', 'hp_max_2' etc) events setup?
Nothing too special, they each add their own corresponding component groups. Here's an example:
{
"component_groups": {
"hp_max_1": {
"minecraft:health": {"max": 1}
},
"hp_max_2": {
"minecraft:health": {"max": 2}
},
// ...
},
"events": {
"hp_max_1": {
"add": {
"component_groups": ["hp_max_1"]
}
},
"hp_max_2": {
"add": {
"component_groups": ["hp_max_2"]
}
},
// ...
}
}
Can you add a placeholder for a score, that still works together with execute if statements
execute if score max_hp max_hp matches 2 run event entity @a hp_max_2
execute if score max_hp max_hp matches 3 run event entity @a hp_max_3
...
In this example 'max_hp' is a placeholder for a score, and a scoreboard as well