#Setitem error

1 messages · Page 1 of 1 (latest)

cursive crescent
#

[Scripting][error]-InvalidArgumentError: Unexpected type passed to function argument [0]. Expected type: number at <anonymous> (custom_hearts.js:59)

world.beforeEvents.itemUse.subscribe(data => {
const player = data.source;
const item = data.itemStack;
if (!player || !item) return; // Ensure player and item are defined

const tags = player.getTags();

if (item.typeId === "minecraft:iron_ingot" && player.getTags().includes("chq:iron_heart")) {
    if (item.amount >= 4 && !isCooldownActive(player.id, 'iron')) {
        system.run(() => {
            const inv = player.getComponent('inventory').container;
            item.amount === 4 ? item = undefined : item.amount -= 1;
            inv.setItem(player.selectedSlot, item);
            player.triggerEvent('chq:knockback_resistance');
        });

        system.runTimeout(() => {
            player.triggerEvent('chq:remove_knockback_resistance');
        }, 15 * 1000);

        setCooldown(player.id, 'iron');
    }
}

});

naive plaza
#

which line is line 59

#

@cursive crescent

#

doesnt matter but

#
world.beforeEvents.itemUse.subscribe(data => {
    const player = data.source;
    const item = data.itemStack;
    if (!player || !item) return; // Ensure player and item are defined

    const tags = player.getTags();

    if (item.typeId === "minecraft:iron_ingot" && player.getTags().includes("chq:iron_heart")) {
        if (item.amount >= 4 && !isCooldownActive(player.id, 'iron')) {
            system.run(() => {
                const inv = player.getComponent('inventory').container;
                item.amount === 4 ? item = undefined : item.amount -= 1;
                inv.setItem(player.SelectedSlotIndex, item);
                player.triggerEvent('chq:knockback_resistance');
            });

            system.runTimeout(() => {
                player.triggerEvent('chq:remove_knockback_resistance');
            }, 15 * 1000);

            setCooldown(player.id, 'iron');
        }
    }
});
#

its SelectedSlotIndex

#

not SelectedSlot

#

@cursive crescent

cursive crescent
#

Thanks

naive plaza
#

np

#

mb i forgot to capitalise the S

cursive crescent
#

Wait

#

[Scripting][error]-TypeError: 'item' is read-only at <anonymous> (custom_hearts.js:58)

#

@naive plaza

slow belfry
cursive crescent
#

Is this replace data.itemStack to data.itemStack?.clone or separated const

#

@slow belfry

cursive crescent
#

Wait never mind

#

It works just replace it