Does PlayerRemoving() and BindToClose() fire after the last player leaves?

Does anyone know if PlayerRemoving() and BindToClose() fire after the last person in the server leaves? I tested it and PlayerRemoving() doesn’t work when the LAST PLAYER leaves. Also, when the last person leave does that make length of Players = 0, so it’s impossible to save data using BindToClose? Thanks.

To my knowledge, PlayerRemoving() fires whenever someone leaves the game. Not when the last person leaves.

game:BindToClose() fires when you end the game (Shutdown, etc) – For doing last player, do this one.
players.PlayerRemoving() fires when 1 of the player leaves.

1 Like

So it doesn’t fire when the last person leaves, but only when another person leaves while there is still someone in the server? Thanks

But when i’m trying to save data it doesn’t get the last player because #Players:GetPlayers() = 0 when using BindToClose() I think.

Like @LocalWE stated PlayerRemoving fires when a player leaves, BindToClose fires when there are no players left/ the game shutsdown.

But does PlayerRemoving fire when the last person leaves? No one has elaborated on that. The answer is no- right?

Yes, It does. Technically it fires right before someone leaves.

But I tried it in-game and it doesn’t fire when the last person leaves. The server just closes itself before PlayerRemoving() can fire I think. Correct me if i’m wrong please.

EDIT: Nvm, I think it does fire- just that you cannot make reference to objects in workspace because they are deleted- only players remain and their data, etc.

Do you mind sending the code? I understand your trying to save data, are you using DataStore?

I think the reason why it’s not working is because i’m trying to save builds on the map in the server, but when the last person leaves the workspace gets deleted with all of its children. That’s probably why it isn’t saving the data for builds properly, right?

Err, You cant save a game to studio. So that would explain why its not working. If you interact with the game while playing like lets say blowing up something, whenever the server shuts down that’s it. A game cant save to studio. The logic doesn’t really workout.

No, I mean I have a saving system that saves builds that players have placed on the map. But when the last person leaves, the workspace deletes all of its children so you can’t REFERENCE to those builds because they get removed when the last person leaves. Do you know what I mean? So I can’t save those builds because they have been removed.

Ah, I see what your trying to say. It should still work though, It might be something in the script.

I think that the workspace gets deleted and the only thing that remains might be StarterGui stuff. This is just theory thought, mere speculation until one of the engineers confirm this. But I believe that my saving doesn’t work because there is no workspace left to refer too. In that case, I will try to find a workaround.

Also my script works fine, it works when the second last player leaves the server- saves perfectly, but when only the last player leaves it doesn’t save for whatever reason. That’s why I speculate workspace becomes unavailable after the last player leaves.

I cant really say what the issue is without seeing the script. I would recommend checking out this post as it might have an answer.

I guess the real question is does workspace and its children get removed/deleted prior to the firing of BindToClose() or PlayerRemoving() when there is only 1 player in the server?

Im not sure, I know BindToClose() fires prior to server shutdown but as for before workspace is gone is a mystery to me. How are you saving the plot?

Well i’ve got some values in the builds that are stored in ReplicatedStorage, including their names which tell me information about the particular item. When the player leaves, the builds left on the map gets saved to prevent exploiting we save right before they leave- but i’m afraid for the last player there might not be a workspace to refer too because as I do believe, BindToClose() and PlayerRemoving() are the last executed code on the server, but it does not guarantee the saving of workspace. In fact, I think they remove workspace because the server is about to close.