#why is my filter not working?

1 messages · Page 1 of 1 (latest)

vagrant geyser
#
function removeItemFromDynamicProperty(player, number) {
    world.sendMessage(number)
    const dy = player.getDynamicProperty('backpack');
    if (!dy) {
        world.sendMessage("Backpack is empty or not defined.");
        return;
    }
    const backpack = JSON.parse(dy);
    const { slots, items, amounts, randoms } = backpack;
    world.sendMessage(`Before: ${slots} /// ${items} /// ${amounts} /// ${randoms}`);
    const index = slots.indexOf(number);
    if (index === -1) {
        world.sendMessage(`Item with number ${number} not found in backpack.`);
        return;
    }
    slots.splice(index, 1);
    items.splice(index, 1);
    amounts.splice(index, 1);
    randoms.splice(index, 1);
    const updatedBackpack = { slots, items, amounts, randoms };
    world.sendMessage(`After: ${slots} /// ${items} /// ${amounts} /// ${randoms}`);
    player.setDynamicProperty('backpack', JSON.stringify(updatedBackpack));
}
#
[2024-05-28 20:02:07:988 ERROR] [Scripting] TypeError: cannot read property 'indexOf' of undefined
leaden lark
#

What is slots

#

I see it’s meant to be an array, but is it being defined correctly?

vagrant geyser
leaden lark
#

You’re passing it into the function as a string

leaden lark
#

The stringify is then taking that as a string

#

And it is being read as a string

#

Ah, also; you’re saving the data as an array of objects

#

But you’re reading the data as just an object

vagrant geyser
leaden lark
leaden lark
vagrant geyser
leaden lark
#

Unless you want it as an array

leaden lark
vagrant geyser
leaden lark
#

Console warn your saved data

vagrant geyser
leaden lark
#

So an array of objects

leaden lark
leaden lark
#

That does not work

vagrant geyser
leaden lark
#

Because you are attempting to destructure an array of objects as though it is just an object

leaden lark
vagrant geyser
leaden lark
#

Wdym

vagrant geyser
#

how can I remove all the line now?

leaden lark
#

Remove what line

vagrant geyser
# leaden lark Remove what line

a line which includes the number that I choose is the number that and in the line I want to recover and supp all the line c possible? if yes how can I do it

leaden lark
#

I do not understand

vagrant geyser