#Help with random button system

1 messages · Page 1 of 1 (latest)

pliant minnow
#

how do i make like a shop system? i mean a form that every 10min or manually resets and puts 4 random buttons in the form, so if before was there 4 buttons, these 4 buttons get deleted and replaced by other 4 random buttons

waxen kernel
#

To run it every 10 minutes you can use system.runInterval

system.runInterval(() => {
  // run code to reset buttons
}, 20 * 60 * 10)
#

To choose random buttons you could make an array with the different buttons, choose 4 of them randomly and save them in a scoreboard, a database or something like that

#

Then you could create the buttons based on the ones that were selected when you open the shop form

pliant minnow
gaunt ermine
#

Here I made a function that randomly select a specified number of elements from the input array and return them as a new array.

#
function arrayRndm(input,amount) {
  input = input.slice()
  const chosen = []
  while (amount > 0 && input.length > 0) {
    amount --
    const picker = Math.floor(Math.random()*input.length)
    chosen.push(input[picker])
    input.splice(picker,1)
  }
  return chosen
}```
#

Example usage:

#
const array = ["red","orange","yellow","green","blue","aqua","pink","purple"]

console.log(arrayRndm(array,4)) //return 4 random elements
console.log(array) //original array is unaffected```
gaunt ermine
#

With this, you can use an array to save the buttons you wanted.

then for each existing element that has been chosen, you can add them as buttons to your ui-form.

gaunt ermine
#

On second thought, I think you also need a way to know what buttons the player is pressing.

Since the button kept randomly changing, its function and what it does also changed.

pliant minnow
hardy monolith
#

no point

#

just use the array

pliant minnow
#

like if the button is called test1 it detects that you pressed test1

hardy monolith
#

array[result.selection]

#

thatll return which button u pressed

pliant minnow
#

but the buttons keep randomly changing

hardy monolith
#

yup thats why i just said how to do it

gaunt ermine
#

Oh yeah right, it's simpler than I thought.

array[result.selection]

That should work.

gaunt ermine
#

@pliant minnow do you know how to make a form? @minecraft/server-ui

pliant minnow
#

yes i know that at least

gaunt ermine
#

Okay... for the delay.. we could use the system.runInterval

#

and each time it fires, it create a new form for the shop

gaunt ermine
pliant minnow
#

k, so i'm going to use a compass for example to open the form and do the function, then what like, how do i modify the function to it to work as the way that i wanted

gaunt ermine
#

to open the form using a compass, then we'll be using itemUse event

#

give me some time

#

I'm coding on a phone

pliant minnow
#

world.beforeEvents.itemUse.subscribe((data) => {
const player = data.source;
const item = data.itemStack;
server.system.run(async () => {
if (item.typeId == "minecraft:compass") {
UISHOP(player);
}
})
});

#

i can use that

gaunt ermine
#

yup

pliant minnow
#

k, now what do i need to modify in the function to it to work

gaunt ermine
#

Are you using ActionFormData or ModalFormData?

pliant minnow
#

ActionFormData

gaunt ermine
#

k

#

Try this, cant check error since debug bot is down

#
function arrayRndm(input,amount) {
  input = input.slice()
  const chosen = []
  while (amount > 0 && input.length > 0) {
    amount --
    const picker = Math.floor(Math.random()*input.length)
    chosen.push(input[picker])
    input.splice(picker,1)
  }
  return chosen
}

//Button List
const buttonList = [
  "wheat",
  "apple",
  "diamond",
  "iron_ingot",
  "netherite_ingot"
]

//Global Variable
let button;
let form;

//each "/reload" reset the shop
ui_change()
function ui_change() {
  let button = arrayRndm(buttonList,4)
  let form = new newActionFormData()
  form.title('shop test')
  button.forEach(e=>{
    form.button(e)
  })
}

//each 10 minutes reset the shop
system.runInterval(()=>{
  ui_change()
},12000)

//Item use Compass
world.afterEvents.itemUse.subscribe((data) => {
    const player = data.source;
    const item = data.itemStack;
    system.run(async () => {
        if (item.typeId == "minecraft:compass") {
            form.show(player)
        }
    })
});
pliant minnow
#

k give me a sec

gaunt ermine
#

You need to manually put an import tho

#

since i forgot

pliant minnow
#

it says that the button is not being read

#

i already have this

#

idk if i need to add something more

gaunt ermine
#

I fixed it

#

after manual testing

#
function arrayRndm(input,amount) {
  input = input.slice()
  const chosen = []
  while (amount > 0 && input.length > 0) {
    amount --
    const picker = Math.floor(Math.random()*input.length)
    chosen.push(input[picker])
    input.splice(picker,1)
  }
  return chosen
}

//Button List
const buttonList = [
  "red",
  "orange",
  "yellow",
  "green",
  "blue",
  "purple",
  "pink"
]

//Global Variable
let button;
let form;

//each "/reload" reset the shop
world.afterEvents.worldInitialize.subscribe((e) => {
    ui_change()
});
function ui_change() {
  button = arrayRndm(buttonList,4)
  form = new ActionFormData()
  form.title('shop test')
  button.forEach(e=>{
    form.button(e)
  })
  world.sendMessage('test')
}

//each 10 minutes reset the shop
system.runInterval(()=>{
  ui_change()
},12000)

//Item use Compass
world.afterEvents.itemUse.subscribe((data) => {
    const player = data.source;
    const item = data.itemStack;
    system.run(async () => {
        if (item.typeId == "minecraft:compass") {
            form.show(player)
        }
    })
});
#

try this sample

#

don't forget the import

#
import {
    world,
    system
} from "@minecraft/server"
import {
    ActionFormData
} from "@minecraft/server-ui"
pliant minnow
gaunt ermine
#

Oh that's your import setup? ok, give me a sec

#

actually nvm, you already redefined the world in there

#

this should already work ( I hope )

pliant minnow
gaunt ermine
#

you can change it to other thing

pliant minnow
#

like this?

gaunt ermine
#

yes

pliant minnow
gaunt ermine
#

import it

#

wait no

#

do server.system.runInterval()

#

I realized

world.afterEvents.worldInitialize.subscribe((e) => {
    ui_change()
});

is unnecessary

#

simplify it into ui_change()

pliant minnow
#

i already fixed it, also is there any way that when the shop changes buttons, it closes the menu?

#

and also, how do i detect when i clicked a button bia his name

#

i want that when i press for example orange, it says orange or something like that

#

to detect whatr buttom i'm pressing

gaunt ermine
#
function arrayRndm(input,amount) {
  input = input.slice()
  const chosen = []
  while (amount > 0 && input.length > 0) {
    amount --
    const picker = Math.floor(Math.random()*input.length)
    chosen.push(input[picker])
    input.splice(picker,1)
  }
  return chosen
}

//Button List
const buttonList = [
  "red",
  "orange",
  "yellow",
  "green",
  "blue",
  "purple",
  "pink"
]

//Global Variable
let button;
let shop;

//each "/reload" reset the shop
ui_change()

function ui_change() {
  button = arrayRndm(buttonList,4)
  shop = new ActionFormData()
  shop.title('shop test')
  button.forEach(e=>{
    shop.button(e)
  })
}

//each 10 minutes reset the shop
server.system.runInterval(()=>{
  ui_change()
},12000)

//Item use Compass
world.afterEvents.itemUse.subscribe((data) => {
    const player = data.source;
    const item = data.itemStack;
    server.system.run(async () => {
        if (item.typeId == "minecraft:compass") {
            shop.show(player).then(r=>{
                if (r.canceled) return
                const select = button[r.selection]
                world.sendMessage(select)
            })
        }
    })
});
#

whenever you select something in a ActionFormData Minecraft returns it as a number.
Use that to return the index in the button variable.

#

In this code example, I broadcast the selection into the chat

pliant minnow
#

Yes but i don't want that, i want that when i select a button bia his name it does a title or a command

gaunt ermine
#

Player name or the button name?

pliant minnow
#

Cuz if i do this

#

it only detects te button that is on 1st place

#

not the button himself

gaunt ermine
#

...

pliant minnow
#

button name

#

like if the first button says hello, i want a command with only that button that says hello, idk how to explain but i think you know what i mean

gaunt ermine
#

You see the chat?

#

blue button give "blue"

#

red button give "red"

pliant minnow
#

yeah but that says in chat the button that you selected, but i want to do a command depending on each button that i select

gaunt ermine
#

use the const select

#

its a string

#

pressing yellow button will gave "yellow" (string) inside const select

#

the 'string' itself depends on the buttonList

pliant minnow
#

wut, idk if you know what i mean, like i want a custom command for each button and also idk how to use the const select...

#

and also how do i give the buttons an image, like in the button system that you gave me

gaunt ermine
#

just test it

#

bruh

#

you're making me nuts, thanks.

#

Can we continue tomorrow?

#

Gotta sleep man

pliant minnow
#

k, but can you confirm if is like this?

gaunt ermine
#

no

#

thats wrong

little elm
pliant minnow
#

i think i got it

gaunt ermine
#
function arrayRndm(input,amount) {
  input = input.slice()
  const chosen = []
  while (amount > 0 && input.length > 0) {
    amount --
    const picker = Math.floor(Math.random()*input.length)
    chosen.push(input[picker])
    input.splice(picker,1)
  }
  return chosen
}

//Button List
const buttonList = [
  ["kill","textures/items/bone"],
  ["diamond","textures/items/diamond"],
  ["emerald","textures/blocks/command_block"],
  ["idk",undefined],
  ["empty",undefined]
]

//Global Variable
let button;
let shop;

//each "/reload" reset the shop
ui_change()
function ui_change() {
  button = arrayRndm(buttonList,4)
  shop = new ActionFormData()
  shop.title('shop test')
  button.forEach(e=>{
    shop.button(e[0],e[1])
  })
}

//each 10 minutes reset the shop
server.system.runInterval(()=>{
  ui_change()
},12000)

//Item use Compass
world.afterEvents.itemUse.subscribe((data) => {
    const player = data.source;
    const item = data.itemStack;
    server.system.run(async () => {
        if (item.typeId == "minecraft:compass") {
            shop.show(player).then(r=>{
                if (r.canceled) return
                const select = button[r.selection][0]
                if (select === "kill")
                player.kill()
                if (select === "diamond")
                player.runCommand('give @s diamond')
            })
        }
    })
});
pliant minnow
#

bruh, i just got it and you send me this lmao

#

thanks btw

gaunt ermine
#

I added the texture thing

#

that you wanted

#

now let me live

pliant minnow
#

i noticed

#

Thanks

gaunt ermine
pliant minnow
#

k np

#

also one final question of this and the i'll let you live, when i do the /reload in here, is there any way to put a command in there to reset a scoreboard or something?
like player.runCommandAsync("scoreboard players set Time-Left shop 600");

gaunt ermine
#

no

#

I didn't use a scoreboard to set the interval delay between resets

little elm
gaunt ermine
#

damn, people are using vsc/bridge that can easily highlight their code for any property and method available.

And I'm here using my phone, memorizing every method and property to minimize the amount of time needed to open the docs.