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.
Basically, BindToClose() runs before the server shuts down, so. Yes! It will run when the last player leaves.
No, nothing is deleted when the servers about to close. It’s only when the server does close. It’s like this script below you can use to optimize servers uptime, so when people leave its up for an extra minute before shutting down.
game:BindToClose(function() wait(60) end)
So if I use wait(num) in BindToClose, I can save the last player’s data using PlayerRemoved() during that wait interval? Thanks for the insight
Anyone know if this is possible, that I can use wait(num) in BindToClose so I can buy time to save the player’s data in PlayerRemoving()?
Thanks everyone, it worked! Just use wait() in BindToClose() and the saving will continue in PlayerRemoving(), it will run the code there!
After 30 seconds the game will shutdown regardless of any length of wait you have stated inside the BindToClose function.