To how many seconds should I do it to then?
I use 5 retries, and pass the retry number as the wait time. (So all retries are completed in about 15 seconds)
I also use an autosave function which spreads the autosave calls from all players automatically over 2 minutes
Sadly, the gamebindtoclose function does nothing:
game:BindToClose(function()
for _, Player in ipairs(game.Players:GetPlayers()) do
local SaveData = {
Time = Player.leaderstats.Time.Value,
RobuxDon = Player.leaderstats.RobuxDon.Value
}
local success, errormessage = pcall(function()
DataStore:SetAsync(Player)
end)
if not success then
local Retries = 0
repeat
local worked = pcall(function()
DataStore:SetAsync(Player)
end)
Retries += 1
warn(Retries)
task.wait(Retries)
until
worked or Retries >= 5
end
end
end)
Only the retry message in the playerremoved event prints only once. The bindtoclose doesnāt print at all.
Iām assuming this was a solo play test?
If the player removed event fired there wouldnāt be any players in the game when bind to close ran.
Add a print at the start of the bind to close function to check when it runs (you could print players:GetPlayers() )
Hi,
My replies have been linked twice in this topic, so i thought Iād chip in with my suggestions.
I did not say you shouldnāt use BindToClose, but rather data should not be saved on BindToClose. This is because whenever the server is shut down, Players.PlayerRemoving is fired for that player. So, should data be saved in your Players.PlayerRemoving connection and on BindToClose, you have two sets of data being written for the same player, and further requests are more likely to be dropped. Alternatively, should all the players have been kicked by the time BindToClose is run, your code within that block will also do nothing.
To fix this, you need to keep the server running for as long as possible in the game environment to ensure requests are processed. To prevent requests from also being dropped, you should create your own queue system to feed requests one by one to the store.
In one of my replies from a previous topic which has been linked here, I mentioned how data store requests are processed asynchronously, so your code continues running before data write operations have been fully processed. This causes the server to close everything once BindToClose has finished running; everything related to that game server, including data store requests, regardless of whether or not they are processed.
Try the following code in your BindToClose function. Itāll wait 5 seconds in studio and 30 seconds (the limit) in game servers.
game:BindToClose(function(_reason)
task.wait(if game:GetService("RunService"):IsStudio() then 5 else 30)
end)
would also like to note that @Wigglyaa and @CreditsMix have both mentioned some of my points already. Thanks!
Also, if you also want to use different behaviour depending on the reason the game shut down, you can get the reason for the server shutdown as a parameter to BindToClose.
Yeah it was
Alright I will try this thank you
Iāll try out your advice thanks koip