#need help with this PlayerLeave event

1 messages · Page 1 of 1 (latest)

eternal hedge
#

This isn’t working as intended. It’s supposed to remove all active raids when the last player leaves the world, but the cleanup isn’t triggering.

world.afterEvents.playerLeave.subscribe((event) => {
    if (world.getAllPlayers().length === 0) {
        system.run(() => {
            console.log("[RaidCore] Last player left: removing all active raids...");

            const overworld = getOverworldSafe();
            if (!overworld) return;

            for (const [key, raid] of activeRaids) {
                try {
                    overworld.runCommand(`setblock ${Math.floor(raid.loc.x)} ${Math.floor(raid.loc.y)} ${Math.floor(raid.loc.z)} minecraft:air`);
                } catch (e) {
                    if (raidcoreConfig.DEBUG) console.warn("[RaidCore] failed clearing core on last player leave:", e);
                }

                try {
                    overworld.runCommand(`kill @e[tag=raidmob_${raid.id}]`);
                } catch (e) {
                    if (raidcoreConfig.DEBUG) console.warn("[RaidCore] failed killing raid mobs on last player leave:", e);
                }

                removeRaidHologram(raid.loc);

                try {
                    overworld.runCommand(`tag @e[tag=raidcore_active] remove raidcore_active`);
                } catch (e) {
                    if (raidcoreConfig.DEBUG) console.warn("[RaidCore] failed removing raidcore_active tags on last player leave:", e);
                }

                activeRaids.delete(key);
            }

            saveActiveRaids();

            console.log("[RaidCore] All active raids removed after last player left.");
        });
    }
});
stray dew
#

i don't think it would trigger because when the last player leaves, the world unloads

radiant gazelle
#

Use beforeEvents it should work... The afterEvents run after the action is done and beforeEvents run before the action is done

fallow sphinx
#

beforeEvents is read only tho

#

so might need system.run, which won't work

radiant gazelle
#

I works i think

eternal hedge
#

But i fixed it.