#actionformdata help

1 messages · Page 1 of 1 (latest)

deep orbit
#
"dependencies": [
    {
        "module_name": "@minecraft/server",
        "version": "1.15.0"
    },
    {
        "module_name": "@minecraft/server-ui",
        "version": "1.3.0"
    }
]
import { ActionFormData } from "@minecraft/server-ui";

    if (item.typeId == "test:team") {
        const form = new ActionFormData()
        form.title("Team Selection")
        form.body("Choose a Team")
        form.button("Team Prime")
        form.button("Team 1")
        form.button("Team 2")
        form.button("Team 3")
        form.button("Team 4")
        form.button("Team 5")

        form.show(player).then((response) => {
            switch (response.selection) {
                case 0:
                    player.addTag("team0")
                    break;
                case 1:
                    player.addTag("team1")
                    break;
                case 2:
                    player.addTag("team2")
                    break;
                case 3:
                    player.addTag("team3")
                    break;
                case 4:
                    player.addTag("team4")
                    break;
                case 5:
                    player.addTag("team5")
                    break;
            }
        })
    }```

I've never worked with actionform before, nor have I ever used the server-ui module. No errors have been popping up for me, and I'm on 1.21.51, using stable.
I'm not sure why this test code isn't working for me, can someone explain where I'm going wrong?
elfin forum
#

It's cos where you have player defined you don't actually have player defined and you're not even using server which is needed for actually attaching your item to an item you use function here's the code you would need for a form to open when you right click with an item ```js
import {world} from "@minecraft/server"
import {ActionFormData} from "@minecraft/server-ui"
world.afterEvents.itemUse.subscribe((data)=>{
const player = data.source
const item = data.itemStack
if(item.typeId === "your item ID goes here"){
ui(player)
}
})
function ui(player){
const form = new ActionFormData()
.title("test")
.body("example body text")
.button("example button")
.show(player).then((response)=>{
if(response.canceled) return
if(response.selection=== 0){
player.sendMessage("example button pressed")
}

})

}

Also update your server version from 1. 15.0 to 1. 19.0 as that is the latest stable
deep orbit
#

I'll try this new script as soon as I can, but for the record, this message is a shortened version of the .js file.

The full file is hundreds of lines long with a bunch of irrelevant stuff as it's code for other items. My item and player variables are defined, there are no errors that come up saying that they are not, and it wouldn't make sense as all the other items work just fine.

I'm also on 1.21.51 at the moment for temporary reasons, so I don't think 1.19.0 would even work for me.