#how to set an item to the second hand of the player?

1 messages · Page 1 of 1 (latest)

hard cargo
#

how to set an item to the second hand of the player?

#

i tried this but it doesnt work becaus the second hand doesnt count with them i guess :

 const offhandItem = player.getComponent("inventory").container.getSlot(35);```
jolly yew
#
const offHand = player.getComponent("equippable").getEquipment("offhand")```
#

it returns undefined or an ItemStack instance

#

and to set an item just

player.getComponent("equippable").setEquipment("offhand", ItemStack)```
hard cargo
# jolly yew and to set an item just ```js player.getComponent("equippable").setEquipment("of...
system.runInterval(() => {
  for (const player of world.getAllPlayers()) {
    const inv = player.getComponent("inventory").container;
    const equip = player.getComponent("equippable");

    // --- Get items ---
    const mainHand = inv.getItem(player.selectedSlotIndex);
    const offHand = equip.getEquipment("Offhand");

    // --- Check for magnet ---
    const isMainMagnet = mainHand?.typeId === "minecraft:stick";
    const isOffMagnet = offHand?.typeId === "minecraft:stick";
    const holdingMagnet = isMainMagnet || isOffMagnet;

    // --- Sneak to move magnet to offhand ---
    if (isMainMagnet && player.isSneaking) {
      if (!offHand) {
        const clonedItem = mainHand.clone();
        equip.setEquipment("Offhand", clonedItem);
        inv.setItem(player.selectedSlotIndex, undefined);
      }
      continue;
    }
  })```
#

i made this but the equip.setEquipment("Offhand", clonedItem); doesnt work

jolly yew
#

just use mainHand instead of clonedItem

#

and it should work