What I mean by the title is... How does BindToClose work... Suppose I lose internet connection and cannot connect again.. then when I put SetAsync in bind to close, how does it actually save..? Is it suppose to.... use some psychic service I never knew about LOL or... idk.. puts the data in a table then before internet loss, saves it... There has gotta be a method to this BindToClose...
Some people say that it takes your old value but that makes no sense... I'd like to know how BindToClose was ever made...
In my internet connection case, it should wait 30 seconds before closing... What if the internet still doesn't come on in 30 seconds, does it save it???
Does the BindToClose and setasync restore data offline?
I tried testing this by using:
game:BindToClose(function() DataStore:SetAsync(p.UserId, values) end)
I still cannot understand the code behind BindToClose, i'd like a good old explanation, thank you.
BindToClose
will fire once there are no players on the server and the server needs to shut down. It will stop the server from closing for up to 30 seconds so that it has time to run the function. Once the function runs or 30 seconds have passed, the server will close.
BindToClose
is similar to OnClose
(now deprecated) in that they both stall the server from shutting down. The reason why we use BindToClose
rather than OnClose
is that there was a problem with OnClose
. The issue was that after the function was over, it would immediately close the game, even if there were other scripts that were also using OnClose
. BindToClose
will not close the game until every single function ran (or 30 seconds is over).
--Script 1 game.OnClose = function() print("Closing Server") end --Script 2 game.OnClose = function() CleanUp() wait(3) SaveData() print("Finished") end
Closing Server
In the above example, script 1 ran and printed a message in the output, but the second script did not get to finish!
--Script 1 game:BindToClose(function() print("Closing Server") end) --Script 2 game:BindToClose(function() CleanUp() wait(3) SaveData() print("Finished") end)
Closing Server Finished
Now, both functions ran to completion because the server waited for both functions to finish before closing.
It's recommended that you save all your unsaved data with BindToClose
in case of an unexpected crash or close. Here is an example of how that would look. In this example, we are making a tycoon game and we're saving cash.
local DataStoreService = game:GetService("DataStoreService") local money = DataStoreService:GetDataStore("Cash") game:BindToClose(function() if not game:GetService("RunService"):IsStudio() then --Don't run if we're in studio mode. local players = game:GetService("Players"):GetPlayers() for _,v in pairs(players) do local userId = v.UserId local success, result = pcall(function() --pcall in case something goes wrong... Money:SetAsync(userId, v.leaderboard.Cash.Value) end) if not success then warn(result) end end end print("Finished") end)
Finished
Leaderboards are probably legacy by now. I'm just using them as an example.
Hope it helps!