#slider
1 messages · Page 1 of 1 (latest)
I want to change the slider option according to how many are coal in the player's inventory
Use [0]
Since it's at the start of an array index
Also, just looping the inventory
function itemAmount(itemId) {
let amount = 0;
const container = player.getComponent("inventory").container;
for (let i = 0; i < container.size; i++) {
const item = container.getItem(i)
if (item?.typeId !== itemId) continue;
amount += item.amount
}
return amount;
}
how do i combine it with
making this into a function would be better imo
also i think return will stop the loop
and not let the rest of the code run
Wait, I accidentally typed return instead of continue
or u can just do const [slider] = result.formValues
for each new formValue, u add a new property to the array
Just add
const coalAmount = itemAmount("minecraft:coal")
And use that inside .slider method
function komur(player) {
const modal = new ModalFormData();
modal.title( `§eKömür Sat` );
modal.slider( "test", 1, getItemAmount( player, "minecraft:coal" ), 1 );
modal.show( player ).then(
(response) => {
const [ coal ] = response.formValues;
console.warn( coal );
},
);
};
function getItemAmount(player, itemId) {
let amount = 0;
const inventory = player.getComponent( "minecraft:inventory" ).container;
for (let i = 0; i < inventory.size; i++) {
const item = inventory.getItem(i);
if (item?.typeId == itemId) amount += item.amount;
};
return amount;
};
Missing ) at getItemAmount
where?
i can’t see stuff better on mobile lol
modal.slider( "test", 1, getItemAmount( player, "minecraft:coal" ), 1 );
or whatever he did
fixed!
Debug Result
There is an error in this [code](#1134467833896648704 message):
[36m<repl>.js[0m:[33m7[0m:[33m19[0m - [31merror[0m[30m TS2488: [0mType '(string | number | boolean)[] | undefined' must have a '[Symbol.iterator]()' method that returns an iterator.
[7m7[0m const [ coal ] = response.formValues;
[7m [0m [31m ~~~~~~~~[0m
Debug (Stable)
i hate strict mode
Working
Thanks
yay!