How could I use BindToClose() to handle shutting down and saving data?

Right now I use an autosaver system that occurs every 60 seconds, but when I shutdown data is rerolled 60 seconds back and a lot could happen within that period (such as gaining something rare). I have heard of BindToClose() before but I haven’t looked into it. But after realising people complaining about losing a minute of data, I was suggested to use BindToClose(), so I have a few questions:

  1. How do I use BindToClose() exactly? In the Dev Hub, there’s an example:
    image
    Are the players kicked from the game where it says print(2*2) or are they still in the game until print(“Done”) is reached?

  2. If I increase the length the wait(3) to lets say 30 seconds, is there a chance that players when shutdown can rejoin the same server since technically the server is still alive?

  3. If I use a loop within BindToClose() to save every players data, should this cause queues and possible data failures? Should I use wait(2) per iteration, note that my main save data function uses OrderedDataStores for leaderboard stuff, I could potentially just add a BoolValue to indicate if it’s necessary to save data to the leaderboard since leaderboards only reflect what the players data is, so next time they do appropriately save, they will appear.

  4. With BindToClose() being added, should I remove this 60 seconds autosaver feature? Since let’s say for example, data could have been saved 2 seconds ago, and then the BindToClose function could cause redundant saving, I could perhaps add a tick() value just to see how long ago data was saved for that player.

I believe that most of these are answered on the api page itself, but I can answer these to the best of my knowledge.

  1. You can put whatever your saving function is inside of the BindToClose function. Roblox will keep the server open until all BindToClose functions are completed, or until it has to wait more than 30 seconds. So if your function does not complete, roblox will still shut the server down after 30 seconds.

  2. That would not happen because as I stated above, after 30 seconds roblox will shutdown the server regardless of whether the function/ loop has completed.

  3. I am not that good with datastores, but I figure you would just save every players data when that happens, just don’t add to many waits or it could take too long meaning some peoples data won’t be saved by the time the server closes.

  4. No. It is good to save every certain interval just to prevent data loss.

3 Likes

I see! Does PlayerRemoving fire before BindToClose? Or does PlayerRemoving not fire at all?

I have no idea about that.
Below is just my best guest, seeing as there isn’t much documentation on that.

I believe though that if you shutdown from the website, BindToClose will be fired before PlayerRemoving.
Seeing as the only other time BindToClose will be fired is when the last player leaves a server, in that case I believe that PlayerRemoving would fire first.

1 Like