#1.6.0-beta (1.20.30) Entity runCommand on player target.

1 messages · Page 1 of 1 (latest)

bleak mantle
#

How do I create a script that when an entity is hit a command is run on the player that hit the entity?

#
import { world, system } from "@minecraft/server";

world.before.events.entityHit.subscribe(data => {
    const npc = data.hitEntity;
    const player = data.entity;

    if (data.hitBlock) {
        return;
    }

    if (npc.hasTag('tag')) {
        player.runCommand('say test');
    }
});```
#

I think its like this, I need to test

#

well that didn't seem to work.

green yew
#
import { world, system } from "@minecraft/server";

world.afterEvents.entityHitEntity.subscribe(data => {
    const npc = data.hitEntity;
    const player = data.damagingEntity;


    if (npc.hasTag('tag')) {
        player.runCommand('say test');
    }
});```
bleak mantle
#
import { world, system } from "@minecraft/server"

world.afterEvents.entityHitEntity.subscribe(ev => {
    const { damagingEntity, hitEntity } = ev;
    damagingEntity.runCommand('I ran something!');
});```
#

I figured out how to do it I think

#

I have to setup VSCode though

green yew
#

Installing the types would help a ton

bleak mantle
#
import { world, system } from "@minecraft/server"

world.afterEvents.entityHitEntity.subscribe(ev => {
    const { damagingEntity, hitEntity } = ev;
    if (hitEntity.name === 'minecraft:pig') {
        console.log(`Entity ${damagingEntity.name} hit a pig`);
        damagingEntity.runCommand('say I hit a pig!');
    }
});
#

for some reason this isn't working

#

When I hit a pig entity it should run the command as me, saying "I hit a pig"

#

1.6.0-beta (1.20.30) Entity runCommand on player target.

green yew
#

.typeId, not .name

bleak mantle
#

Thanks for the help, the big issue was I didn't use "require"

#

or import