#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)

celest plume
#

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 } ]

#
//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)```
bronze saffronBOT
#
Debug Result

There are 3 errors in this [code](#1102781645418942464 message):

<repl>.js:7:24 - error TS2552: Cannot find name 'items'. Did you mean 'item'?

7         let randItem = items[Math.floor(Math.random() * items.length)]
                         ~~~~~

  <repl>.js:8:13
    8         let item = new ItemStack(Items.get(randItem.item), randItem.amount)
                  ~~~~
    'item' is declared here.
<repl>.js:7:57 - error TS2552: Cannot find name 'items'. Did you mean 'item'?

7         let randItem = items[Math.floor(Math.random() * items.length)]
                                                          ~~~~~

  <repl>.js:8:13
    8         let item = new ItemStack(Items.get(randItem.item), randItem.amount)
                  ~~~~
    'item' is declared here.
<repl>.js:9:9 - error TS2322: Type '"slot"' is not assignable to type 'ItemLockMode'.

9         item.lockMode = "slot" //this will set the lockmode
          ~~~~~~~~~~~~~

sick scarab
#

it the Items capitalization intentionnal?

celest plume
#

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

winged thunder
#
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);