Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

game.Players.PlayerRemoving:connect(function(player) is only firing sometimes?

Asked by 5 years ago

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.

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

Ad
Log in to vote
0
Answered by
nilVector 812 Moderation Voter
5 years ago
Edited 5 years ago

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)
0
It still didn't output removed for me sonicfan0405 18 — 5y
0
Are you getting any errors? Can you give more context? What type of script is this, and where is it located? nilVector 812 — 5y

Answer this question