#Welcome Message Issue
1 messages · Page 1 of 1 (latest)
Sorry, but I don't quite understand what you're saying. I'm new to Minecraft scripting. Could you please explain how to fix it?
world.afterEvents.playerJoin
Oh, Alright, I'll test it now.
Script/Code:
import { world, system } from "@minecraft/server";
let playerJoinStatus = new Map();
world.afterEvents.playerJoin(eventData => {
const player = eventData.player;
const playerName = player.name;
if (!playerJoinStatus.has(playerName)) {
const playerCount = playerJoinStatus.size + 1;
player.sendMessage(`Welcome to the server, §a${playerName}§r! You are currently the §a${playerCount}th§r member!`);
playerJoinStatus.set(playerName, true);
}
});```
**Error Message**:
```js
[Scripting][error]-Plugin [Welcome Message - 3.0.0] - [main.js] ran with error: [TypeError: not a function at <anonymous> (main.js:15)
]```
I think I did something wrong...
You still need the .subscribe
right after the event name
[Scripting][error]-TypeError: cannot read property 'name' of undefined at <anonymous> (main.js:7)```
Is there scripting form thing for minecraft bedrock?
playerJoin doesnt have a player property, but it has the playerName property.
so you could do something like this
world.afterEvents.playerJoin.subscribe(data => {
const playerName = data.playerName
const player = world.getPlayers({name: playerName})[0]
//...
})
I heard playerSpawn triggers things twice, am I wrong
I was reading messages, ive seen someone say smth like that
alr
Maybe its code logic
The message isn't guaranteed to display for the player, since the load time can vary between experiences. That's gonna be the other dilemma here. Even if you switch it to use the playerSpawn event.
true
Could just defer it by 30 secs
Yes , use runTimout
Code/Script:
import { world } from "@minecraft/server";
let playerJoinStatus = new Map();
world.afterEvents.playerJoin.subscribe(data => {
const playerName = data.playerName;
const player = world.getPlayers({ name: playerName })[0];
if (player && !playerJoinStatus.has(playerName)) {
const playerCount = playerJoinStatus.size + 1;
player.sendMessage(`Welcome to the server, §a${playerName}§r! You are currently the §a${playerCount}th§r member!`);
playerJoinStatus.set(playerName, true);
}
});```
It doesn't show any errors and no message has been displayed to chat.
Or should I change the manifest version?
(I'm very new so I have no idea)
import { world, system } from "@minecraft/server";
let playerJoinStatus = new Map();
const DELAY_TICKS = 600; // 30 seconds at 20 ticks per second
world.afterEvents.playerSpawn.subscribe(async eventData => {
const player = eventData.player;
const playerName = player.name;
if (player.initialSpawn) {
if (!playerJoinStatus.has(playerName)) {
const playerCount = playerJoinStatus.size + 1;
// Delay sending the message
await system.waitTicks(DELAY_TICKS);
player.sendMessage(`Welcome to the server, §a${playerName}§r! You are currently the §a${playerCount}th§r member!`);
playerJoinStatus.set(playerName, true);
}
}
});
This uses the playerSpawn event. Checks if the player spawning is spawning for the first time in the world. Then it proceeds its process to send the message to the player, but not until waiting 30 seconds to give the player time to fully load so they see the message.
Keep in mind, that not everyone may be able to load within 30 seconds. So there will still be cases where some players do not see the message. You would have to essentially defer it longer or find another solution to validate if they loaded.
could maybe do a system.runInterval that keeps checking if theyre undefined?
so something like this should work
world.afterEvents.playerJoin.subscribe(event => {
const start = world.getDynamicProperty("rk:start")
if (!start) return;
const startInfo = JSON.parse(start)
if (startInfo[0] <= startInfo[1]) {
let player;
const startInterval = system.runInterval(() => {
player = getPlayerByName(event.playerName);
if (player) {
system.clearRun(startInterval)
if (player.hasTag("startedRanks")) return;
setRank(player, startInfo[0]);
player.addTag("startedRanks")
world.setDynamicProperty("rk:start", JSON.stringify([startInfo[0] + 1, startInfo[1]]));
}
}, 20)
} else {world.setDynamicProperty("rk:start", false)}
})
That would probably present the same problem, but more consistently since the playerJoin event would trigger a return from the object before they spawn.
But testing and verifying would confirm for sure.
wdym
function getPlayerByName(username) {
for (const player of world.getPlayers()) {
if (player.name === username) {
return player;
}
}
return;
}
this is my function
you are basically just checking if the name matches ever second until it does
and if that happens then player is defined
so where is the issue
Player is defined, yes, but are they fully loaded.
as in you can interact with them in the script?
just check for isValid then
Different platforms, networks included, have different load times on the users end is what I'm pointing at.
function getPlayerByName(username) {
for (const player of world.getPlayers()) {
if (player.name === username && player.isValid) {
return player;
}
}
return;
}
here
Bump
yeah so you are checking every second until their game is loaded?
am I missing something here
No, I'm saying that we currently don't have a sufficient way of knowing if they are truly loaded. Meaning that we can't determine truly if they see the world on their screen. The game may recognize they have joined but there screen may still be rendering and so the message would be missed.
okay so add a system.runTimeout at the clearRun and make it wait like 5 seconds from when that happens
I doubt itll take that long
Exactly. You would need to differ the time which is what I said in my post back in 2024 lol.
okay but theres no way you'll have to wait that long
I've seen cases where it takes up to a minute
and I'm pretty sure it'll appear in chat anyways no?
It wont
well thats a little odd but ig you can still do normal functions with the player
Yea, you can. It's one of those situations where you just can't hit the mark for everyone. But you can make it count for the majority and the minority will just be fine without lol.