#How to detect player join for first time
1 messages · Page 1 of 1 (latest)
wdym? like first ever join and it won't trigger when he joins again?
Yes
just add a tag.
How I detect player tag
if (player.hasTag('test'))
Thanks
You can use the initialSpawn, why use a tag?
How I use initialSpawn can you make example code
Wait
world.afterEvents.playerSpawn.subscribe((data) => {
let player = data.player;
let Spawn = data.initialSpawn;
if (!Spawn) {
//Code here
}
})
console.warn(${player.name} has Joined in first time)
Wait a minute, is my grammar wrong?
where ?
${player.name} has joined in first time
maybe ${player.name} has joined first is better
but everyone understands in any case, so it's okay
Ok
afaik initialSpawn runs everytime you joined the server, not first time.
Yes it is.
Then that's not what he wants.
That's why I said he should use tag
Yes
Yeah, your answer is right.
Tag option working
when I use initialSpawn it was not working for first and not for another time
Wew, I just knew it
//===============Dependency==========
import { system,world,Player } from "@minecraft/server";
world.afterEvents.playerJoin.subscribe(e => {
const {player} = e;
if (!player.hasTag('joined'))
var now = new Date();
var currentDateAndTime = now.toISOString();
console.log(`${e.playerName} New player joined the game ${currentDateAndTime}`)
I tried but I am getting error
world.afterEvents.playerSpawn.subscribe(event => {
const { player, initialSpawn } = event;
if (initialSpawn) {
var now = new Date();
var currentDateAndTime = now.toISOString();
console.log(`${player.name} New player joined the game ${currentDateAndTime}`)
}
});```
🗿
I haven't tested it yet :'v
I have a lot of things to do at home
Btw, Where do you get e.playerName from?
What does event mean?
mybad i just copy pasted it
._.
But this will work every time when a player join I want run only for first time
nope i'm wrong
it runs when he joins the world for the very first time
to test that create new world
Where you use this function
if (player.hasTag('test'))
``` ?
I tested not working was running every time
I made screen record wait I am sending
it works for me so idk what you mean
Where you use this function
if (player.hasTag('test'))
``` ?
inside of initialSpawn statement
I run this code on server
//===============Dependency==========
import { system,world,Player } from "@minecraft/server";
world.afterEvents.playerSpawn.subscribe(event => {
const { player, initialSpawn } = event;
if (initialSpawn) {
var now = new Date();
var currentDateAndTime = now.toISOString();
console.log(`${player.name} New player joined the game ${currentDateAndTime}`)
}
});
world.afterEvents.playerSpawn.subscribe(event => {
const { player, initialSpawn } = event;
if (initialSpawn) {
if (player.hasTag('test')) {
player.sendMessage("TEST");
}
}
});```
- TypeScript:
import { world, PlayerSpawnAfterEvent } from "@minecraft/server";
world.afterEvents.playerSpawn.subscribe(({ player, initialSpawn }: PlayerSpawnAfterEvent): void => {
if (!initialSpawn || player.getDynamicProperty("hasJoinedBefore")) return;
player.sendMessage("You joined for the first time.");
player.setDynamicProperty("hasJoinedBefore", true);
});
- JavaScript:
import { world } from "@minecraft/server";
world.afterEvents.playerSpawn.subscribe(({ player, initialSpawn }) => {
if (!initialSpawn || player.getDynamicProperty("hasJoinedBefore")) return;
player.sendMessage("You joined for the first time.");
player.setDynamicProperty("hasJoinedBefore", true);
});
What different between
world.afterEvents.playerJoin.subscribe
world.afterEvents.playerSpawn.subscribe
Use the script above your chat
playerJoin triggers when a player first joins (even if they aren't yet loaded) and playerSpawn triggers when a player spawns (either they died & respawned or its they joined the world, initialSpawn == false is if they respawned after a death, initialSpawn == true is if they first joined the server, now initialSpawn == true is even if they left & rejoined)
Also playerJoin triggers like the initialSpawn == true but without the player being fully loaded in