I'm not sure what is going on but when a player leaves, I have the console ouput "removed". Sometimes it prints it and sometimes it doesn't. I'm just looking for a simple answer as to why it is only sometimes working when a player leaves. Thanks.
I don't know why it wouldn't work all the time, but I guess you could use ChildRemoved instead?http://wiki.roblox.com/index.php?title=API:Class/Instance/ChildRemoved
There might be another answer, but that's all I could think of.
This is because functions connected to PlayerRemoving
do not execute when the last player in the server leaves.
To get around this, you must utilize BindToClose()
. BindToClose allows Roblox to call callback functions right before shutting down (which it will attempt to do when the last player leaves).
Here's how you should generally handle players leaving:
local function playerLeaving(player) -- handle player leaving here end game.Players.PlayerRemoving:Connect(playerLeaving) game:BindToClose(function() for _, player in pairs(game.Players:GetPlayers()) do playerLeaving(player) end end)