#correct way to use "player.getComponent('equippable')"?

1 messages · Page 1 of 1 (latest)

hollow estuary
#

Question above

What I tried: if (player.getComponent().get Equipment().itemStack) {} Same error message.

Error message: [Scripting][error]-TypeError: not a function at <anonymous> (o.js:13)

module v 1.8.0

Import {
world,
system,
Entity,
Player
} from "@minecraft/server";

function mainTick() {
world.afterEvents.playerBreakBlock.subscribe((ev) => {

    const player = world.getPlayers();
    
    const getEntityEquippableComponent = player.GetComponent('equippable');
    
    if (getEntityEquippableComponent.getEquipment('Offhand').itemStack.typeId == "minecraft:totem_of_undying") {
        console.warn("C");
    } else {
        console.warn("D");
    }
});

}
system.run(mainTick);

astral oak
#

Are you using stable or beta?

#

This is for beta```js
world.afterEvents.playerBreakBlock.subscribe(({ player }) => {
const offhand = player.getComponent('equippable').getEquipment('Offhand');
if (offhand?.typeId == "minecraft:totem_of_undying") {
console.warn("C");
} else {
console.warn("D");
}
});

#

Idk if it works with stable

hollow estuary
#

it doesn't work, I just get else result even if the Totem is in the main hand

astral oak
#

Oh wait

#

I didn't see you wanted to use offhand

#

Ok now its fixed

hollow estuary
#

{player}, offhand?

#

What's the function of those two things?

compact moss
# hollow estuary What's the function of those two things?
  1. { player }: This syntax is known as object destructuring in JavaScript. It allows you to extract specific properties from an object. In this case, it's extracting the player property from the object passed to the playerBreakBlock event handler. This syntax is shorthand for const player = event.player;, assuming event is the object passed to the event handler.

  2. "offhand": This is a string representing the name of a property. In the code, it's used as a key to access a property of an object. Specifically, it's used to access the typeId property of the offhand object, which is retrieved from the equippable component of the player.