#How to unsubscribe to a player that is subscribed within playerSpawn afterEvent?
1 messages · Page 1 of 1 (latest)
There's lots of ways to handle this. That does your code currently look like
So when a player joins a world, a bunch of functions are assigned to that player object that joined. So when that player leaves the world, the assigned functions for other player objects get updated as well. It's like their connected even though they're not.
It's not good practice to directly assign functions to the Player object. Do you have an example of the code that's giving you issues?
Instead of assigning the player object, assign their player id. You can get that when they leave and handle the functions accordingly.
If it's necessary.
I gotchu. Thank you so much!
I think that's exactly what's causing the issue
Can you show me an example of how it's done?
Can I send you the code?
Sure. Post it here
So it's being setup here on playerSetup.js and assigns each player that spawned a function. However, when a player leaves the world, it somehow reassigns the function to another active player in the world and passing the data from the previous player which is incorrect even though I've assigned that function based from the player ID.
For example: Both players are riding their dragons while mid-air. When one of them leaves the world, the function of the other player updates and flags them that they're not riding a dragon, which results to the dragon falling from the sky.
Should I store that function to a variable for each player?
//playerSetup.js
import { world } from "@minecraft/server"
import { followOwnerHandlerOnOwnerJoin } from "../dragons/followOwnerMidFlight";
import { flightController } from "./dragonFlightController";
import { armorEffects } from "./armorEffects";
world.afterEvents.playerSpawn.subscribe((event) => {
const player = event.player;
const firstSpawn = event.initialSpawn
const playerId = player.id;
if (firstSpawn) {
player.runCommand('scoreboard players set @s weapon_cooldown 0');
//Call this function located at world/dragons/followOwnerMidflight.js to re-pair the player that joined with their tamed dragons to allow the dragons to follow the player while in mid-air
//followOwnerHandlerOnOwnerJoin(player.id);
//Call this function located at world/player/dragonFlightController.js to get its current ridden dragon every tick for this player only
flightController(player.id);
////Call this function located at world/player/dragonFlightController.js to apply armor set effects to the player that joined
//armorEffects(player.id);
}
});```
What I think is that it updates the player id assigned in the function parameter to another active player in the world
@neat pulsar here's the playerSetup.js
dragonFlightController.js
export function flightController(playerId) {
let checkingIntervals = {};
// Subscribe to the playerLeave event to handle player leaving the world
const onPlayerLeave = world.afterEvents.playerLeave.subscribe((event) => {
const leavingPlayerId = event.playerId;
if (leavingPlayerId === playerId) {
// Check if there is an interval associated with the leaving player
if (checkingIntervals[leavingPlayerId]) {
// Clear the interval associated with the leaving player
system.clearRun(checkingIntervals[leavingPlayerId]);
// Remove the interval from the checkingIntervals object
delete checkingIntervals[leavingPlayerId];
}
// Unsubscribe from the playerLeave event to avoid memory leaks
onPlayerLeave.unsubscribe();
}
});
world.getAllPlayers().forEach(thisPlayer => {
if (thisPlayer.id === playerId) {
// Assign the interval to checkingIntervals[playerId]
checkingIntervals[playerId] = system.runInterval(() => {
// Code for checkingActive...
});
// Rest of the code...
}
});
}
Will try this later. Thank you!
I edited it to create an object where it can store the players ID with the interval ID from runInterval so it knows which ID is associated to which player when they leave the world.
Haven't tested the modifications but should be sufficient. It will point you in the right direction.
Yes. Thanks for the insight. Appreciate the help 💯🔥