I am using a simple Datastore that takes names and values from a folder and saves it. The issue is that I get too many requests only on shutdown and not when players leave. It saves data perfectly fine on player leaving but when I use BindToClose which is the same exact code, I get too many requests. I have tried looking on the wiki for more information about this but didn’t find anything. Thanks for all the help.
local DatastoreService = game:GetService("DataStoreService")
local Datastore = DatastoreService:GetDataStore("Version1")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
Players.PlayerAdded:Connect(function(Player)
local PlayerData = ServerStorage.PlayerData:Clone()
PlayerData.Parent = Player
local Names = Datastore:GetAsync(Player.UserId.."Names")
local Values = Datastore:GetAsync(Player.UserId.."Values")
if Names ~= nil and Values ~= nil then
for i, v in pairs(PlayerData:GetChildren()) do
if v.Name == Names[i] then
v.Value = Values[i]
end
end
end
end)
Players.PlayerRemoving:Connect(function(Player)
local PlayerData = Player:WaitForChild("PlayerData")
local Names = {}
local Values = {}
for i, v in pairs(PlayerData:GetChildren()) do
table.insert(Names, v.Name)
table.insert(Values, v.Value)
end
Datastore:SetAsync(Player.UserId.."Names", Names)
Datastore:SetAsync(Player.UserId.."Values", Values)
print("Saved"..Player.Name.." 's Data")
end)
game:BindToClose(function()
for i, Player in pairs(game.Players:GetChildren()) do
local PlayerData = Player:WaitForChild("PlayerData")
local Names = {}
local Values = {}
for i, v in pairs(PlayerData:GetChildren()) do
table.insert(Names, v.Name)
table.insert(Values, v.Value)
end
Datastore:SetAsync(Player.UserId.."Names", Names)
Datastore:SetAsync(Player.UserId.."Values", Values)
end
print("Saved all players data")
end)
BindToClose() only fires on a server’s close. instead I suggest saving all data in PlayerRemoving and then in BindToClose() put a wait(3) and nothing mor, this’ll get the server uptime enough time to save any potential closing applications that haven’t saved yet.
I don’t think that the “wait(3)” would do anything as the server has 30 seconds for all functions to discontinue and finish before the server is fully shutdown.
Well wait(3) is what I used to do. Now I use DataStore2 and may end up creating my own DataStore for my own benefit(as lately I’ve found myself saving global data a lot) but I digress. BindToClose() is what I’ve used in the past and it is what fixed this exact issue long ago and haven’t had that problem since. However I don’t recommend trying to save players data inside BindToClose() as regarding normal DataStores the only time you should ever need to get and set data is PlayerAdded and PlayerRemoving there shouldn’t really be any need to save data elsewhere which includes BindToClose
In that tutorial I have linked in my latest reply, Colbert had mention and talked about yielding or waiting inside of BindToClose and also using BindToClose for saving data.