#HELP java bow ding

1 messages · Page 1 of 1 (latest)

blazing hornet
#
world.afterEvents.projectileHit.subscribe(event => {
  const { source: player } = event;
  const playerHit = event.getEntityHit()?.entity;
  const playerHitHealth = playerHit?.getComponent("minecraft:health");
  const soundOptions = { volume: 0.4, pitch: 0.5 };
  if(player?.typeId !== "minecraft:player" || playerHit?.typeId !== "minecraft:player") return;
  if(player.getComponent("minecraft:equipment_inventory").getEquipment("mainhand")?.typeId == "minecraft:bow") {
      player.sendMessage(`§7Has dado >>§b${playerHit.name ?? "Bloque"} §l§aHP§r§f: §c${Math.round(playerHitHealth.current) / 2}§f/§a${Math.round(playerHitHealth.value) / 2}`);
      player.playSound("random.orb", soundOptions);
  }
});
#

Mistakes:
Ding does not sound
N/A appears on the life tracker

#

(Google translator)

lunar sable
#

i found a code for that
||```js
// Script example for ScriptAPI
// Author: JaylyDev https://github.com/JaylyDev
// Project: https://github.com/JaylyDev/ScriptAPI
import { Player, world, MinecraftEntityTypes } from "@minecraft/server";

world.afterEvents.entitySpawn.subscribe(({ entity }) => {
// Since you cannot retrive projectile infomation from projectileHit event, we have to
// subscribe to entitySpawn event to compare with the projectile information fired by projectileHit event.
if (entity.typeId !== MinecraftEntityTypes.arrow.id) return;

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

if (hitInfo?.entity instanceof Player && source instanceof Player && projectile === entity) {
  /**
    * @type {import("@minecraft/server").PlayerSoundOptions}
    */
  const soundOption = {
    volume: 0.4,
    pitch: 0.5,
  }
  
  source.playSound("random.orb", soundOption);
  world.afterEvents.projectileHit.unsubscribe(callback);
};

});
});