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);
}```