#Having Trouble Detecting Damage

1 messages · Page 1 of 1 (latest)

cosmic socket
#

i was trying to use the after event for entityHitEntity but i noticed that if the player hits the entity fast it wont damage them, but still count as a hit, how can i detect when an entity is damaged? i cant seem to figure out how to use entityDamageSource, if thats even the right one

white lagoon
#

Get the time in-between hits with a map

cosmic socket
#

ok i will try that, thanks

cosmic socket
#

i used a map to store their health and check if it changed so that works, but i also want to beable to get the damage source, like if a player uses fire aspect, i will get the owner of the damage source, is that possible?

cosmic socket
#

thank you

pure heath
#
mc.world.afterEvents.entityHitEntity.subscribe(async (entityHit) => {
  const
    player = entityHit.damagingEntity,
    target = entityHit.hitEntity;
 

} )
cosmic socket
pure heath
#

As you can see, there is the player and the target

#

Target is the entity that receives the damage

cosmic socket
#

yes but it detects when the player hits the entity and i only want to detect damaging or hurting the entity

pure heath
#

And the player is the entity that deals the damage

cosmic socket
#

make a hit detection for zombies so i can add points

cosmic socket
#

i got it now, thanks

pure heath
#

Or do you want me to detect the player's attacks on a zombie?

cosmic socket
#

im trying to have it so when the player damages or kills the zombie it will add points

#

i think i got it working now tho

pure heath
#

@cosmic socket

#
mc.world.afterEvents.entityHurt.subscribe(event => {
    
  const hurtEntity = event.hurtEntity, hitEntity = event.damageSource, damage = event.damage
  
  if (hurtEntity.typeId == "minecraft:zombie" || hurtEntity.typeId == "minecraft:piglin" || hurtEntity.typeId == "minecraft:husk" || hurtEntity.typeId == "minecraft:drowned") {  
    player.runCommandAsync('scoreboard players add @s ssak_score 1')
  }
} )
pure heath
cosmic socket
#

im not new to scripting, just minecrafts api

pure heath
pure heath
#

Just the math is very similar

cosmic socket
#

ive been messing with it all day, i think i got the gist of interacting with entities

#

and world events

pure heath
pure heath
cosmic socket
#

thats pretty much what i was doing in the hitEntity event

#

i couldnt find any vids on the scripting api so i decided to try to learn from the docs

#

this is what i was doing before

world.afterEvents.entityHitEntity.subscribe(async event => {
    const player = event.damagingEntity;
    const target = event.hitEntity;
    const targId = target.id;
    const playerName = player.name;
    const targName = format(target.typeId);
    const targHealth = target.getComponent(EntityComponentTypes.Health).currentValue;
    world.sendMessage(playerName + ' hit ' + targName);
    if (player.typeId === item('player') && targName === "Zombie"){
        if (targHealth === 0) {
            if (zedHealthMap.has(targId)) {
                if (zedHealthMap.get(targId)) {
                    player.runCommandAsync('scoreboard players add @s points 200');
                    zedHealthMap.delete(targId);
                }
            }
        }else{
            if (zedHealthMap.get(targId) !== targHealth){
                player.runCommandAsync('scoreboard players add @s points 10');
            }
            zedHealthMap.set(targId, targHealth);
        }
    }
});

world.afterEvents.entityDie.subscribe(event => {
    const entity = event.deadEntity.id;
    if (zedHealthMap.has(entity)){
        if (zedHealthMap.get(entity)){
            zedHealthMap.delete(entity);
        }
    }
});```
pure heath
#

If you need more help with this, you can call me