Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions lib/observePlayer.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--!strict

type Callback = (player: Player) -> (() -> ())?
type Callback = (player: Player) -> ((Enum.PlayerExitReason) -> ())?

local Players = game:GetService("Players")

Expand All @@ -25,32 +25,25 @@ local function observePlayer(callback: Callback): () -> ()
local playerRemovingConn: RBXScriptConnection

local cleanupsPerPlayer: { [Player]: (exitReason: Enum.PlayerExitReason) -> () } = {}
local inflight: { [Player]: Enum.PlayerExitReason } = {}

local function OnPlayerAdded(player: Player)
if not playerAddedConn.Connected then
return
end

task.spawn(function()
inflight[player] = Enum.PlayerExitReason.Unknown
local cleanup = (callback :: any)(player)
if typeof(cleanup) == "function" then
if playerAddedConn.Connected and player.Parent then
cleanupsPerPlayer[player] = cleanup
else
task.spawn(cleanup, inflight[player])
task.spawn(cleanup, Enum.PlayerExitReason.Unknown)
end
end
inflight[player] = nil
end)
end

local function OnPlayerRemoving(player: Player, exitReason: Enum.PlayerExitReason)
if inflight[player] then
inflight[player] = exitReason
end

local cleanup = cleanupsPerPlayer[player]
cleanupsPerPlayer[player] = nil
if typeof(cleanup) == "function" then
Expand Down Expand Up @@ -80,7 +73,7 @@ local function observePlayer(callback: Callback): () -> ()

local player = next(cleanupsPerPlayer)
while player do
OnPlayerRemoving(player, inflight[player])
OnPlayerRemoving(player, Enum.PlayerExitReason.Unknown)
player = next(cleanupsPerPlayer)
end
end
Expand Down