#Detector [RESOLVED]

1 messages · Page 1 of 1 (latest)

proud gust
#

How do I detect when a player shoots an entity using bow, crossbow and trident?

thorny lily
#
import {
    Player,
    world
} from '@minecraft/server';

world.afterEvents.entitySpawn.subscribe(({
    entity
}) => {
    if (entity.typeId !== 'minecraft:arrow' || entity.typeId !== 'minecraft:trident') return;

    const callback = world.afterEvents.projectileHitEntity.subscribe((arg) => {
        const {
            source,
            projectile
        } = arg;
        const hitInfo = arg.getEntityHit();

        if (hitInfo?.entity instanceof Player && source instanceof Player && projectile === entity) {
            const soundOption = {
                volume: 0.4,
                pitch: 0.5,
            }

            source.playSound('random.orb', soundOption);
            world.afterEvents.projectileHitEntity.unsubscribe(callback);
        };
    });
});
#

@proud gust

proud gust
#

It was "projectileHitEntity"

thorny lily
thorny lily
#

Actually this is made by jayly

proud gust
thorny lily
#

U can change hitInfo?.entity instanceof Player

#

To hitInfo?.entity.typeId == 'custom: entity'

thorny lily
proud gust
high totemBOT
#

Suggestion for @proud gust

Description
Creates a 'ding' sound when a player hits the other player with a bow.

Credits
These scripts were written by JaylyDev

proud gust
#

Detector [RESOLVED]

high totemBOT
# thorny lily ```js import { Player, world } from '@minecraft/server'; world.afterEve...

Debug result for [code](#1329063822958264363 message)

Compiler Result

Compiler found 1 errors:

<REPL0>.js:9:48 - error TS2367: This comparison appears to be unintentional because the types '"minecraft:arrow"' and '"minecraft:trident"' have no overlap.

9     if (entity.typeId !== 'minecraft:arrow' || entity.typeId !== 'minecraft:trident') return;
                                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lint Result

There are no errors from ESLint.

proud gust
# high totem Debug result for [code](https://discord.com/channels/523663022053392405/13290638...
import { Player, world } from "@minecraft/server";

world.afterEvents.entitySpawn.subscribe(({ entity }) => {
    if (entity.typeId !== 'minecraft:arrow' && entity.typeId !== 'minecraft:trident') return;

    const callback = world.afterEvents.projectileHitEntity.subscribe((arg) => {
        const { source, projectile } = arg;
        const hitInfo = arg.getEntityHit();

        if (hitInfo?.entity && source instanceof Player && projectile === entity) {
            const soundOption = {
                volume: 0.4,
                pitch: 1.0,
            };

            source.playSound("random.orb", soundOption);
            world.afterEvents.projectileHitEntity.unsubscribe(callback);
        }
    });
});```