I made some effort trying to make my script only work when the players 3rd hotbar slot is empty, however I couldn't figure out a solution (probably because I dumb). But Anyways Here's My Script For Reference js import { ItemStack, Items, system, world } from "@minecraft/server"; //Add Items here to put into the randomizer when they go through the block const items = [ { item: "kart:banana_item", amount: 1 }, { item: "kart:coin_item", amount: 1 }, { item: "kart:mushroom_item", amount: 1 }, { item: "kart:green_shell_item", amount: 1 }, { item: "kart:triplemushroom_item", amount: 3 }, { item: "kart:goldenmushroom_item", amount: 10 } ]
#I Don't Know How To Make It So My Script Will Only Work If The Players Hotbar Slot (3) Is Empty
1 messages · Page 1 of 1 (latest)
//No Changes Needed Below, But if does not satisfy what you want it is changeable but might break if you change certain values
let randomizing = new Map()
function randomize(player) {
let time = 15 //How many times the item should be randomized
let n = system.runInterval(() => {
//Randomized item
let randItem = items[Math.floor(Math.random() * items.length)]
let item = new ItemStack(Items.get(randItem.item), randItem.amount)
item.lockMode = "slot" //this will set the lockmode
//Players inventory
let inventory = player.getComponent("minecraft:inventory").container;
inventory.setItem(3, item)
if (time <= 0) {
randomizing.delete(player.name)
system.clearRun(n)
}
time--
}, 5)
}
function getPlayerNames() {
const players = Array.from(world.getPlayers());
var playerNameArray = [];
for (const player of players) {
playerNameArray.push(player.name);
}
return playerNameArray;
}
system.runInterval(() => {
for (let player of world.getAllPlayers()) {
let playerLoc = {
x: Math.floor(player.location.x),
y: Math.floor(player.location.y),
z: Math.floor(player.location.z)
}
let block = world.getDimension(player.dimension.id).getBlock(playerLoc)
if (block.isAir()) {
playerLoc.y += 1;
block = world.getDimension(player.dimension.id).getBlock(playerLoc)
}
if (block.typeId === "kart:question_block_spawner") {
if (!randomizing.has(player.name)) {
return randomize(player)
}
}
}
}, 0)```
Debug Result
There are 3 errors in this [code](#1102781645418942464 message):
[36m<repl>.js[0m:[33m7[0m:[33m24[0m - [31merror[0m[30m TS2552: [0mCannot find name 'items'. Did you mean 'item'?
[7m7[0m let randItem = items[Math.floor(Math.random() * items.length)]
[7m [0m [31m ~~~~~[0m
[36m<repl>.js[0m:[33m8[0m:[33m13[0m
[7m8[0m let item = new ItemStack(Items.get(randItem.item), randItem.amount)
[7m [0m [36m ~~~~[0m
'item' is declared here.
[36m<repl>.js[0m:[33m7[0m:[33m57[0m - [31merror[0m[30m TS2552: [0mCannot find name 'items'. Did you mean 'item'?
[7m7[0m let randItem = items[Math.floor(Math.random() * items.length)]
[7m [0m [31m ~~~~~[0m
[36m<repl>.js[0m:[33m8[0m:[33m13[0m
[7m8[0m let item = new ItemStack(Items.get(randItem.item), randItem.amount)
[7m [0m [36m ~~~~[0m
'item' is declared here.
[36m<repl>.js[0m:[33m9[0m:[33m9[0m - [31merror[0m[30m TS2322: [0mType '"slot"' is not assignable to type 'ItemLockMode'.
[7m9[0m item.lockMode = "slot" //this will set the lockmode
[7m [0m [31m ~~~~~~~~~~~~~[0m
it the Items capitalization intentionnal?
Yes its intentional, It wont work if its lowercase "items" and it's referring to the import "Items". I'm just having issues with this script only working if the players 3 hotbar slot is empty
if( !randomizing.has(player.name)) {
return randomize(player)
}
Idk if it's intentional but if you put return over here, it will only check for 1 player in the world and then exit.
To make it only for empty slot 3, do this after getting inventory:
let inventory = player.getComponent("inventory").container;
if(inventory.hasItem(3)) return;
inventory.setItem(3, item);