#How can i use /scriptevent to open a form
1 messages · Page 1 of 1 (latest)
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
not working
why not working?
remove player from new ModalFormData()
Debug result for [code](#1312590713543983121 message)
Compiler Result
Compiler found 1 errors:
[36m<REPL0>.js[0m:[33m11[0m:[33m19[0m - [31merror[0m[30m TS2345: [0mArgument 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.
[7m11[0m .show(player)
[7m [0m [31m ~~~~~~[0m
Lint Result
There are no errors from ESLint.
@vital ermine
yeah
thats cuz you defined sourceEntity
use sourceEntity
moneyTransfer(sourceEntity)
(({ id, sourceEntity: player)} => {
moneyTransfer(player)
system.afterEvents.scriptEventReceive.subscribe(
({ id, sourceEntity: player }) => {
if (id === 'gui:hi') {
system.run(() => {
moneyTransfer(player);
});
}
});
Shouldn't
why doesnt it work?
nah
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?
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}` : ``}`);
});
}