I was trying to make it so that when using a certain item, the player would dash to wherever they were looking. I asked a friend for help and he gave me this, but when using the item, the dash doesn't work. Does anyone know how to fix this or what's wrong?
import { world, system, ItemStack, Vector, MinecraftItemTypes, Player } from "@minecraft/server";
const COOLDOWN_CATEGORY = "dash_item";
const COOLDOWN_DURATION = 600;
world.beforeEvents.itemUse.subscribe(event => {
const { source: player, itemStack } = event;
if (!(player instanceof Player)) return;
if (itemStack?.typeId !== "deadcore:dash") return;
const cooldownComp = player.getComponent("cooldown");
if (cooldownComp?.isCooldownActive(COOLDOWN_CATEGORY)) {
player.runCommand(`title @s actionbar Dash in cooldown...`);
return;
}
const view = player.getViewDirection();
if (Math.abs(view.y) > 0.4) {
player.runCommand(`title @s actionbar ¡you can't use the dash up or down!`);
return;
}
const speed = 1.5;
const dash = new Vector(view.x * speed, 0, view.z * speed);
player.applyKnockback(dash.x, dash.z, 0.5, 0.5);
player.startItemCooldown(COOLDOWN_CATEGORY, COOLDOWN_DURATION);
player.runCommand(`playsound random.orb @s`);
player.runCommand(`title @s actionbar ¡Dash used!`);
});