#Random Item Every 30 Seconds
1 messages ยท Page 1 of 1 (latest)
const itemList = ItemTypes.getAll().map((item)=>item.id);
function giveRandomItem(player) {
const inv = player.getComponent("inventory")?.container;
if (!inv) return;
//much more cheaper in term of performance
const itemID = itemList[Math.floor(itemList.length * Math.random())];
const item = new ItemStack(itemID,1);
inv.addItem(item);
}
system.runInterval(()=>{
world.getPlayers((player)=>{
//Call function to give random item
giveRandomItem(player);
})
},600) //600 tick = 30 seconds
Do you know how many items existed? Calling Math.random() that many times every 30s with a few players would be a waste. ๐
your way does Math.random() the same amount of times aswell lol
the sort thing every time would be uneccessary though, i can agree on that
ngl i didnt process the every 30 seconds thing and just saw random item
Mine called the Math.random() once per player. wdym?
the itemList is cached outside the function to save performance.
once again, the sort and generally how the list was stored i agree
but we would have called math.random the same amount
you have edited that message like 3 times now ๐
No, we don't.
your Math.random() is within the sort(), iterating the ItemTypes. Meaning, it's being called proportional to how many items ItemTypes returned.
oh that yeah oops misunderstood what you were saying
import it from "@minecraft/server"