#sell item script doesn't work

1 messages · Page 1 of 1 (latest)

pallid jay
#

Im beginner in minecraft scripting and i don't understand why this function doesn't work:

function sell(player, item, cost) {
    const objective = world.scoreboard.getObjective('money');
    const itemCount = player.getComponent("minecraft:inventory").container.toArray()
        .filter(item => item && item.typeId === item)
        .reduce((total, item) => total + item.amount, 0);
    if (itemCount < 1) {
        player.onScreenDisplay.setActionBar('§cError: §rYou do not have this item');
        return;
    }
    giveItem(player, item, -1);
    objective.addScore(player, cost);
    player.onScreenDisplay.setActionBar(`§aSuccess: §rYou have sold §e` + item);
}```
#

i did this before for buying item:

function buy(player, item, cost) {
    const objective = world.scoreboard.getObjective('money');
    if (objective.getScore(player) < cost) {
        player.onScreenDisplay.setActionBar('§cError: §rYou do not have enough money');
        return;
    }
    giveItem(player, item, 1);
    objective.addScore(player, -cost);
    player.onScreenDisplay.setActionBar(`§aSuccess: §rYou have bought §e` + item);
}
feral roostBOT
pallid jay
#
function giveItem(player, itemType, count) {
    const inventory = player.getComponent("minecraft:inventory");

    if (inventory) {
        const itemStack = new ItemStack(itemType, count);
        inventory.container.addItem(itemStack);
    }
}
feral roostBOT
# pallid jay i did this before for buying item: ```js function buy(player, item, cost) { ...

Debug result for [code](#1320552763561611359 message)

Compiler Result

Compiler found 1 errors:

<REPL0>.js:7:5 - error TS2304: Cannot find name 'giveItem'.

7     giveItem(player, item, 1);
      ~~~~~~~~

Lint Result

ESLint results:

<REPL0>.js

1:10 error 'buy' is defined but never used @typescript-eslint/no-unused-vars

polar vigil
#

Alr

pallid jay
#

...

#

:')

polar vigil
#

What the problem U get it?

#

When running the script

pallid jay
#

This ??

polar vigil
#

When this error throwed

#

Can U send full code

pallid jay
#

i just need to repair the sell function line:24

polar vigil
#

Ok wait a moment

pallid jay
#

Okayy thx

polar vigil
#

@pallid jay alr test it

pallid jay
#

That doesn't work x)

polar vigil
#

What's wrong

pallid jay
#

same error

#

Do you know how i can use the minecraft debugger ?

pallid jay
#

yep

spring daggerBOT
#
Info

This bot was created by SmokeyStack for the purpose of making a FAQ bot for the Bedrock Add-Ons Discord Server.

Managing Entries

To manage entries, please make a pull request on GitHub.

Source Code
polar vigil
#

Wait not this

#

@pallid jay

pallid jay
#

nahhh

#

the extension in vscode

#

sry I wasn't clear

vale umbra
#

lmao

polar vigil
pallid jay
#

uh okay

#

dw

vale umbra
polar vigil
vale umbra
polar vigil
vale umbra
pallid jay
#
function sell(player, item, cost) {
    const objective = world.scoreboard.getObjective('money');
    const itemCount = player.getComponent("minecraft:inventory")
        .container.toArray()
        .filter(i => i && i.typeId === item)
        .reduce((total, i) => total + i.amount, 0);

    if (itemCount < 1) {
        player.onScreenDisplay.setActionBar('§cError: §rYou do not have this item');
    } else {
        const inventory = player.getComponent("minecraft:inventory")
            .container;
        for (let slot = 0; slot < inventory.size; slot++) {
            const slotItem = inventory.getItem(slot);
            if (slotItem && slotItem.typeId === item) {
                if (slotItem.amount > 1) {
                    slotItem.amount -= 1;
                    inventory.setItem(slot, slotItem);
                } else {
                    inventory.setItem(slot, null);
                }
                break;
            }
        }
        const currentMoney = objective.getScore(player.scoreboardIdentity);
        objective.setScore(player.scoreboardIdentity, currentMoney + cost);

        player.onScreenDisplay.setActionBar(`§aSuccess: §rYou have sold §e${item}`);
    }
}
vale umbra
#

you should loop for the Whole inven then store it manually