#How can i use /scriptevent to open a form

1 messages · Page 1 of 1 (latest)

vital ermine
#

How?

pallid plover
# vital ermine How?
system.afterEvents.scriptEventReceive.subscribe(data => {
    const player = data.sourceEntity;
    const id = data.id;

  // to open, use /scriptevent open:form
    if (id === "open:form) {
       form(player);
    }
});

function form(player) {
    const form = new ActionFormData()
        .title("title")
        .body(`your body`)

        .button(`button 1`)

        .show(player).then((r) => {
            switch (r.selection) {
                case 0:
                    console.warn(`button 1 pressed`);
                    break;
            }
        });
}```
#

you dont even need to use functions

#

you can do it directly through the if statement

#
system.afterEvents.scriptEventReceive.subscribe(data => {
    const player = data.sourceEntity;
    const id = data.id;

    // /scriptevent open:form to open 
    if (id === "open:form") {
        new ActionFormData()
            .title("title")
            .body("your body")
            .button("button 1")
            .show(player)
            .then(r => {
                if (r.selection === 0) {
                    console.warn("button 1 pressed");
                }
            });
    }
});
#

like that

pallid plover
#

erm

#

errors?

#

did you imported from server-ui?

vital ermine
vital ermine
pallid plover
vital ermine
#

even this is not working

radiant coveBOT
# pallid plover ```js system.afterEvents.scriptEventReceive.subscribe(data => { const player...

Debug result for [code](#1312590713543983121 message)

Compiler Result

Compiler found 1 errors:

<REPL0>.js:11:19 - error TS2345: Argument of type 'Entity' is not assignable to parameter of type 'Player'.
  Type 'Entity' is missing the following properties from type 'Player': camera, clientSystemInfo, inputPermissions, isEmoting, and 29 more.

11             .show(player)
                     ~~~~~~

Lint Result

There are no errors from ESLint.

snow topaz
#

@vital ermine

vital ermine
#

yeah

pallid plover
vital ermine
pallid plover
#

use sourceEntity

#

moneyTransfer(sourceEntity)

vital ermine
scarlet delta
#

system.afterEvents.scriptEventReceive.subscribe(
({ id, sourceEntity: player }) => {
if (id === 'gui:hi') {
system.run(() => {
moneyTransfer(player);
});
}
});

vital ermine
#

@scarlet delta

#

@snow topaz

#

do i need a .json file as well?

snow topaz
#

Shouldn't

vital ermine
#

why doesnt it work?

snow topaz
#

But does it worked?

vital ermine
#

nah

scarlet delta
vital ermine
# scarlet delta can you send the function js file?
function moneyTransfer(player) {
    const players = world.getAllPlayers()
    const result = new ModalFormData(player)
        .title("Money Transfer")
        .dropdown("§fSelect a player§r", players)
        .textField("§7Enter Amount", "§oEnter Amount Here")
        .textField("§7Gift Message", "§oEnter Message Here")
    const [dropdown, amount, message] = result.formValues
    const target = players[dropdown]
    if (player.name === target.name) return player.sendMessage(`§cYou cannot send money to yourself.`)
    if (amount > getScore(player, "money")) return player.sendMessage(`§cInsufficient funds.`)
    else {
        target.runCommandAsync(`scoreboard players add @s money ${textField}`)
        target.sendMessage(`§aYou have received ${textField} from ${player.name}.` + message ? ` §7Attached Message: §a${message}` : ``)
        player.runCommandAsync(`scoreboard players remove @s money ${textField}`)
        player.sendMessage(`§aYou have sent ${textField} to ${target.name}.` + message ? ` §7Attached Message: §a${message}` : ``)
    }
}```
#

Is that gud?

scarlet delta
#

Try this


function moneyTransfer(player) {
    const players = world.getAllPlayers().map(p => p.name);
    const result = new ModalFormData()
        .title("Money Transfer")
        .dropdown("§fSelect a player§r", players)
        .textField("§7Enter Amount", "§oEnter Amount Here")
        .textField("§7Gift Message", "§oEnter Message Here");

    result.show(player).then(response => {
        if (response.canceled) return;

        const [dropdown, amount, message] = response.formValues;
        const targetName = players[dropdown];
        const target = world.getPlayers({ name: targetName })[0];

        if (!target) return console.warn("player not found.")
        if (player.name === target.name) return player.sendMessage("you cant send money to yourself")
        if (amount > getScore(player, "money")) return player.sendMessage("insufficient funds")

        target.runCommandAsync(`scoreboard players add @s money ${amount}`);
        target.sendMessage(`§aYou have received ${amount} from ${player.name}.${message ? ` §7Attached Message: §a${message}` : ``}`);
        player.runCommandAsync(`scoreboard players remove @s money ${amount}`);
        player.sendMessage(`§aYou have sent ${amount} to ${target.name}.${message ? ` §7Attached Message: §a${message}` : ``}`);
    });
}