Does a server end when the last player leaves?

Okay, so basically I have a building save system and it needs to run code after a player leaves, but I am wondering that does a server end and delete itself after the last person leaves?

Or does it finish running any code still executing and then stops itself? Thank you.

1 Like

Try using game.BindToClose()
e.g.

game:BindToClose(function()
    -- Stuff to do before the server closes
end)
2 Likes

I believe so, if you want to make it so it saves when a server is shutting down after the last player leaving or when the server shuts down, BindToClose is what you need

I’m currently using PlayerRemoving, does that execute code before the server ends when the player leaves? I tried it on server and it does. So is it okay if I just use that?

You could, but it’s better to be safe than sorry by also using BindToClose the event you shut down your game or if in some cases the last palyer leaving wont have their builds saved before the server shuts down

So should I replace PlayerRemoving with BindToClose or does BindToClose only execute when the server is about to close? If that’s the case, I will keep PlayerRemoving. Thanks.

I’ve talked about this on a recent post before, but PlayerRemoving won’t exactly work 100% of the time

If your Data Events are connected with local functions, you can connect the BindToClose() function with that

If for the minor reason PlayingRemoving does not work, loop through all the players using the function like so:

game:BindToClose(function()
    for _, Player in pairs(game.Players:GetPlayers()) do
        SaveData(Player)
    end
end)

You’ll still get every Player Instance to save the data from

No no I meant, use both PlayerRemoving AND BindToClose. So it saves for when a palyer leaves and when the Seerver shuts down, that way you can account for every scenario

So BindToClose if i’m correct only executes when the last person leaves, right?

When the last player leaves and when you shutdown the servers. You’ll have to treat BindToClose as a save everyone’s data, as @JackscarIitt mentioned. It does run whe nthe last palyer leaves, but also when you shutdown a specific server/all the servers

Got it. I’ll see if it is usable, thanks.

1 Like

Also keep in mind, to also use PlayerRemoved functions to save data in case of server crash

Will PlayerRemoved still work if the game crashes? Like will it execute before the crash? Thanks

I don’t think so,

But my point is, if you save all players data ONLY before the server closes, if it crashes, all the data is gone.

If you save ONLY when each player leaves, there is a chance that the last player that leaves will not have their data saved

If you use BOTH (Recommended) players will not lose their data in case of crash, and the last player will not have chance of bug when leaving

Also BindToClose doesn’t execute code in server while testing in Studio, or it doesn’t show up in the output. Is this normal because the server still exists in your window, and the code still executes but you just can’t see the output because the server is gone?

Oh I already do that in my game.

BindToClose will still work on both the Studio & Player regardless of where you are

If you wanna see the Output it casts, just add a wait(10) to see your prints so that it can yield before it shuts the game down

1 Like

5 minutes after last player leaves, server dies

No that’s not how it works that’s illegal

BindToClose() can only go up to a maximum of 30 seconds before it truly shuts the current server down

1 Like

Does anyone know how I can see BindToClose() output after shutting down my server window in Test Mode? I think the only way to stop the server is by pressing the “X” button, therefore deleting output. Thanks.