How would I detect when a server closes?

Hello devs,

I was wondering how would I detect when a server closes? Please tell me if this isn’t the appropriate category!

This should go in #help-and-feedback:scripting-support

1 Like

Done. But what about how would I detect whens a server closes?

I think BindToClose would help you with this.

game:BindToClose(function()
     print("Game is shutting down.")
end)
6 Likes

To detect when a server closes I would just use the PlayerRemoving() event and each time the event occurs, I would check the amount of players that are still in the game.
NOTE: I believe there is possibly a more efficient way to do this.

I actually want to detect when a server closes for a server ban thing, so I can unban them each time. So do I just unban them when the player number is 0?

Last thing, how would I check how much players are there?

I believe you would do something like this:
#game.Players:GetChildren

local players = game:GetService("Players")
local playerCount = #players:GetChildren()