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

DataStore - BindToClose or PlayerRemoving?

Asked by 3 years ago

Recently, I've been hearing people say "Use :BindToClose() not .PlayerRemoving()" but I don't entirely understand. Does :BindToClose() also run when a play leaves? I am also confused because .PlayerRemoving() works fine for me and I've never tried :BindToClose() before. They say that :BindToClose() runs when servers shut down so I wonder does it run when players leave. But also, why use :BindToClose() when .PlayerRemoving() has been running upon server shutdown for me.

Thanks in advance.

4 answers

Log in to vote
2
Answered by 3 years ago

The way people are answering this so far is incorrect. You're not delaying server shut down when you use :BindToClose. It just runs all the functions you place inside of it, also called "bound functions" in the article about it, in less than 30 seconds before the server shuts down. For example, the server may shut down before your PlayerRemoving function runs. If you keep that function outside of the :BindToClose but also copy and paste it into :BindToClose you will ensure that the PlayerRemoving function will run, no matter what happens.

Example:

game.Players.PlayerRemoving:Connect(function(player)
    local data = {} -- whatever you want to save
    DataStore:SetAsync(player.UserId, data)
end)

game:BindToClose(function()
    for _, player in pairs(game.Players:GetPlayers()) do -- notice that you can loop through every player left in the server so the function can run for them
        local data = {} -- whatever you want to save
        DataStore:SetAsync(player.UserId, data)
    end
end)

(I used some bad habits in this script, so don't actually use it for your game if you're trying to save data. I'm just trying to show you in the quickest way possible.)

:BindToClose will run 30 seconds before the server shuts down. If the server isn't going to shut down, players leaving are not going to be affected by it. The original PlayerRemoving function will run for them. This is why you want both PlayerRemoving and :BindToClose together. Usually, the server shuts down when there are no players left in the server. Sometimes, however, it can shut down unexpectedly. If there is one player left in the server and they leave, it is possible that the server will shut down before it finishes saving their data. This could also happen while you're playing solo in Roblox Studio. I ran into this issue as well, and my data is saving now all thanks to :BindToClose.

0
So if I shut the server down for an update, lets say, will :BindToClose() fire? IAmNotTheReal_MePipe 418 — 3y
0
Sorry about the late answer but yes, that's when it will fire. AntiWorldliness 868 — 3y
Ad
Log in to vote
1
Answered by
jgftr7 74
3 years ago

So :BindToClose() is for when the game shuts down. But It Matters if you want to wait when player leaves us Player Removing. For More Info go here: http://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

0
So will players' data not save until the server shuts down if I use :BindToClose()? IAmNotTheReal_MePipe 418 — 3y
0
yes lol jgftr7 74 — 3y
0
If I shut down the server for an update, will :BindToClose() fire? IAmNotTheReal_MePipe 418 — 3y
0
well yeah becuase the game is shutting down it doesnt matter how the script just checks if it shuts down jgftr7 74 — 3y
Log in to vote
1
Answered by 3 years ago

Use both. Personally I use :BindToClose to delay the server shutdown and Players.PlayerRemoving to save the data.

game.Players.PlayerRemoving:Connect(function(player)
    -- Code to save data
end)

game:BindToClose(function()
    for _, player in pairs(game.Players:GetPlayers()) do
        player:Kick() -- Kick the player so the player removing function gets triggered
    end
    wait(5) -- Delay for the server shutdown meanwhile the PlayerRemoving event is saving data
end)
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

From what I know, BindToClose only runs when the server is about to shutdown, it’s better to use PlayerRemoving here.

Answer this question