#Help with random button system
1 messages · Page 1 of 1 (latest)
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
idk how to do that, i'm new on this
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```
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.
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.
is there a way to detect what button is the player selecting bia his name?
like if the button is called test1 it detects that you pressed test1
but the buttons keep randomly changing
yup thats why i just said how to do it
Oh yeah right, it's simpler than I thought.
array[result.selection]
That should work.
@pliant minnow do you know how to make a form? @minecraft/server-ui
yes i know that at least
Okay... for the delay.. we could use the system.runInterval
and each time it fires, it create a new form for the shop
For the random 4 button selection we could use this function (I guess)
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
to open the form using a compass, then we'll be using itemUse event
give me some time
I'm coding on a phone
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
yup
k, now what do i need to modify in the function to it to work
Are you using ActionFormData or ModalFormData?
ActionFormData
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)
}
})
});
k give me a sec
it says that the button is not being read
i already have this
idk if i need to add something more
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"
?this
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 )
you have const form already defined there
you can change it to other thing
like this?
yes
import it
wait no
do server.system.runInterval()
I realized
world.afterEvents.worldInitialize.subscribe((e) => {
ui_change()
});
is unnecessary
simplify it into ui_change()
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
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
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
Player name or the button name?
Cuz if i do this
it only detects te button that is on 1st place
not the button himself
...
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
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
use the const select
its a string
pressing yellow button will gave "yellow" (string) inside const select
the 'string' itself depends on the buttonList
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
just test it
bruh
you're making me nuts, thanks.
Can we continue tomorrow?
Gotta sleep man
k, but can you confirm if is like this?
Thats would try to get a boolean from an object
i think i got it
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')
})
}
})
});
Note: I have no idea how to forcefully close the form. So I can't fulfill this one.
The theoretical method is to use damage, but it's unlikely to work every time.
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");
I would use vsc to code as bridge is best for making packs
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.