#nickname
1 messages · Page 1 of 1 (latest)
world.afterEvents.playerSpawn.subscribe(async (data) => {
let { initialSpawn, player } = data
if (!initialSpawn) return
system.runTimeout(() => {
let form = new ModalFormData()
form.title("Write a nickname")
form.textField("nickname", "", player.nameTag)
form.show(player).then((response) => {
let { canceled, formValues } = response
if (canceled) return
let [nickname] = formValues
player.sendMessage("Nickname set to " + nickname)
player.nameTag = `${nickname}`
})
}, 60)
})
```here an example you can use it or change it
thats why its an example
he can change it if he want
does not work
Join word, the script didn't work
try creating a new one
does not work
it's the nameTag not the name of the player who and change
it should be player.nameTag
world.beforeEvents.chatSend.subscribe((data) => {
const { message, sender } = data;
data.cancel = true;
world.sendMessage(`${sender.nameTag}§r >> ${message}`)
});
but it's in your script to see your nameTag in the chat
I'll take a look now
import { world, system} from "@minecraft/server"
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";
world.afterEvents.playerSpawn.subscribe(async (data) => {
let { initialSpawn, player } = data
if (!initialSpawn) return
system.runTimeout(() => {
let form = new ModalFormData()
form.title("Write a nickname")
form.textField("nickname", "", player.nameTag)
form.show(player).then((response) => {
let { canceled, formValues } = response
if (canceled) return
let [nickname] = formValues
player.sendMessage("Nickname set to " + nickname)
player.nameTag = `${nickname}`
})
}, 60)
})
the script doesn't work
@hardy breach
idk maybe there is an issue in your manifest
update your manifest
oh
you need to look at the docs. I think you can't use beta
I'll check now
- Show error
- show hole code
- show manifest
.
there were no errors
import { world, system} from "@minecraft/server"
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";
world.afterEvents.playerSpawn.subscribe(async (data) => {
let { initialSpawn, player } = data
if (!initialSpawn) return
system.runTimeout(() => {
let form = new ModalFormData()
form.title("Write a nickname")
form.textField("nickname", "", player.nameTag)
form.show(player).then((response) => {
let { canceled, formValues } = response
if (canceled) return
let [nickname] = formValues
player.sendMessage("Nickname set to " + nickname)
player.nameTag = `${nickname}`
})
}, 60)
})
Can we see ur manifest?
Maybe add more delay change 60 to 120
nick don't change
do you have other code that use player.nameTag?
No
I think it would simplify things if you just shared your behavior pack here so someone can review it as a whole.
It looks good. I think the issue may be that the timeout for 120 isn't long enough. The spawn event will trigger before you see the world render on your screen. You could still be in the loading screen, loaded, but not able to interact yet. As such, if the form is loaded in this instance then I would imagine it won't show at all when the world finally does render after the loading screen goes away. Try extending the timer and see if that is what is happening.
Ok
If that is the issue and solution, then I would suggest handling it differently. Maybe verify if the user is busy, if that's an option, and having it loop back to create a new instance of the form again, until no longer busy. I say this because the loading time joining a world will vary between people. Some faster than others. You won't be able to make the timeout perfect with those conditions. Every device, and network is different for people so the experience will vary.
It can be a difference of someone loading in the world under 20 seconds and another loading in a world in about 1-1/2 minutes.
Just to put it in perspective.
and how to solve it
Try this:
import { world, system, Player } from "@minecraft/server";
import { ModalFormData } from "@minecraft/server-ui";
/**
* Displays a modal form to set a nickname for the player.
* If the player is busy, recalls the form until the player interacts with it or cancels.
* @param {Player} player - The player to display the form to.
*/
function displayNicknameForm(player) {
/**
* Represents the data structure of the nickname form.
* @type {ModalFormData}
*/
let form = new ModalFormData();
form.title("Write a nickname");
form.textField("nickname", "", player.nameTag);
form.show(player).then((response) => {
const { canceled, formValues, cancelationReason } = response;
if (response && canceled && cancelationReason === "UserBusy") {
// If the player is busy, recall the form
displayNicknameForm(player);
return;
}
if (canceled) return;
const [nickname] = formValues;
player.sendMessage("Nickname set to " + nickname);
player.nameTag = nickname;
});
}
/**
* Event handler for the playerSpawn event.
* @param {object} data - The data object containing information about the event.
*/
world.afterEvents.playerSpawn.subscribe((data) => {
let { initialSpawn, player } = data;
if (!initialSpawn) return;
system.runTimeout(() => {
// Call the function to display the nickname form
displayNicknameForm(player);
}, 60);
});
reduce slow running
//import{world,system}from"@minecraft/server";
world.afterEvents.playerSpawn.subscribe(({player,initialSpawn:spawn})=> {
if(spawn) {
system.run(()=> { //handler privillage
//code.....
}
}
});
It has the potential to show the form before the world renders.
add sleep function
async function sleep(tick) {
new Promise(resolve => system.runTimeout(resolve, tick));
}
That's ideally what I did above.
Here
why not like this?
//import{world,system}from"@minecraft/server";
world.afterEvents.playerSpawn.subscribe(({player,initialSpawn:spawn})=> {
if(spawn) {
system.runTimeout(()=> { //delay 3sec
//code.....
}, 60);
}
});
#1231213575926714368 message
#1231213575926714368 message
hmm
use forceOpen
async function forceOpen(form,player) {
while(true) {
const res = await form.show(player);
if (res.cancelationReason !== "UserBusy") return res;
}
}
I've tried it and it works
That's what I did lol.
oh you right
Player is from @minecraft/server
import {world, player} from "@minecraft/server"
import { ModalFormData } from "@minecraft/server-ui";
/**
-
Displays a modal form to set a nickname for the player.
-
If the player is busy, recalls the form until the player interacts with it or cancels.
-
@param {Player} player - The player to display the form to.
/
function displayNicknameForm(player) {
/*- Represents the data structure of the nickname form.
- @type {ModalFormData}
*/
let form = new ModalFormData();
form.title("Write a nickname");
form.textField("nickname", "", player.nameTag);
form.show(player).then((response) => {
const { canceled, formValues, cancelationReason } = response;
if (response && canceled && cancelationReason === "UserBusy") {
// If the player is busy, recall the form
displayNicknameForm(player);
return;
}
if (canceled) return;
const [nickname] = formValues;
player.sendMessage("Nickname set to " + nickname);
player.nameTag = nickname;
});
}
/**
-
Event handler for the playerSpawn event.
-
@param {object} data - The data object containing information about the event.
*/
world.afterEvents.playerSpawn.subscribe(async (data) => {
let { initialSpawn, player } = data;
if (!initialSpawn) return;system.runTimeout(() => {
// Call the function to display the nickname form
displayNicknameForm(player);
}, 60);
});
import {world, system, Player} from"@minecraft/server"; ```
Yea, Player is from @minecraft/server. That was a clinical error on my part haha. I edited it above to fix that. Oops.
Watchdog simply let's you know if one or more scripts in the behavior pack run slower than anticipated. In some cases you can just ignore that, while with other cases you may need to investigate if it's an ongoing reoccurrence. It typically requires having to change the logic of the code. To refactor it with performance and memory preservation in mind.
It's pretty clear you don't understand much about the API, but that is fine. You just need to start researching about how to optimize code and what are some good practices to implement. There are many different ways to accomplish the same goal but not every way is the most efficient way. This is something you learn as you write code and grow.
I think the first way to start on this is by first looking over the code you have, and understanding what the code is doing line by line.