#How to unsubscribe to a player that is subscribed within playerSpawn afterEvent?

1 messages · Page 1 of 1 (latest)

summer gull
#

When a subscribed player leaves a world, it affects other subscribed players that have assigned functions, how do I fix it?

neat pulsar
#

There's lots of ways to handle this. That does your code currently look like

summer gull
#

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.

neat pulsar
fair linden
#

If it's necessary.

summer gull
#

I think that's exactly what's causing the issue

summer gull
neat pulsar
summer gull
# neat pulsar 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

summer gull
fair linden
# summer gull So it's being setup here on playerSetup.js and assigns each player that spawned ...

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...
        }
    });
}
summer gull
#

Will try this later. Thank you!

fair linden
#

Haven't tested the modifications but should be sufficient. It will point you in the right direction.

summer gull
#

Yes. Thanks for the insight. Appreciate the help 💯🔥