Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is the logic behind BindToClose???

Asked by 5 years ago

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.

0
Your data was placed into the server when you joined. Until you delete the data you have collected in your scripts, it is on the server. It never left. User#21908 42 — 5y
0
BindToClose is not for when you leave the game/disconnect either. PlayerRemoving is for when you disconnect. User#21908 42 — 5y
0
BindToClose is for when the server is going to shut down. The game crashing would not be saved with BindToClose. User#21908 42 — 5y

1 answer

Log in to vote
3
Answered by 5 years ago
Edited 5 years ago

Brief Definition

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.


OnClose vs BindToClose

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.


DataStores and its Relationship with BindToClose

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!

1
LordDragonZord is back!!!!! User#19524 175 — 5y
2
Welcome back. :> TheeDeathCaster 2368 — 5y
0
Thanks Zord greatneil80 2647 — 5y
Ad

Answer this question