Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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)

2 comments

0

It still didn't output removed for me

0

Are you getting any errors? Can you give more context? What type of script is this, and where is it located?

This discussion is preserved as it was. New answers, comments, and votes are closed.