#Welcome Message Issue

1 messages · Page 1 of 1 (latest)

rotund hinge
#

You need to specify if its a before or after event

icy fractal
primal meteor
#

world.afterEvents.playerJoin

icy fractal
icy fractal
# primal meteor world.afterEvents.playerJoin

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...
rotund hinge
#

You still need the .subscribe

primal meteor
#

right after the event name

icy fractal
#

Is there scripting form thing for minecraft bedrock?

primal meteor
plush flicker
#

Use playerSpawn

#

Instead

primal meteor
plush flicker
#

Trigger things twice ?

#

Nah , i use it everything is alright

primal meteor
#

I was reading messages, ive seen someone say smth like that

primal meteor
plush flicker
hollow wind
#

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.

primal meteor
#

true

rotund hinge
#

Could just defer it by 30 secs

plush flicker
#

Yes , use runTimout

icy fractal
# primal meteor playerJoin doesnt have a player property, but it has the playerName property. so...

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)

hollow wind
#
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.

trail rampart
#

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)}
})
hollow wind
#

But testing and verifying would confirm for sure.

trail rampart
#
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

hollow wind
#

Player is defined, yes, but are they fully loaded.

trail rampart
#

just check for isValid then

hollow wind
#

Different platforms, networks included, have different load times on the users end is what I'm pointing at.

trail rampart
#
function getPlayerByName(username) {
    for (const player of world.getPlayers()) {
        if (player.name === username && player.isValid) {
            return player;
        }
    }
    return;
}
#

here

trail rampart
#

am I missing something here

hollow wind
trail rampart
#

I doubt itll take that long

hollow wind
#

Exactly. You would need to differ the time which is what I said in my post back in 2024 lol.

trail rampart
#

okay but theres no way you'll have to wait that long

hollow wind
#

I've seen cases where it takes up to a minute

trail rampart
#

and I'm pretty sure it'll appear in chat anyways no?

hollow wind
#

It wont

trail rampart
#

well thats a little odd but ig you can still do normal functions with the player

hollow wind
#

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.

trail rampart
#

I have an idea for this though

#

its a little bit of a weird rework but

#

you could check their location instead

#

and if it changes then the message displays