I have these 2 files (which I will show soon after explaining), called Leader board and Skips. The Leader board files simply creates a leader board and the skips script creates skips inside the player. I am using datastores on both of these scripts (leader board and skips).
I have recently implemented BindToClose() function in my Data Store.
Leaderboard Script:
local DataStoreService = game:GetService("DataStoreService") local myDataStore = DataStoreService:GetDataStore("LeaderboardSavingSystem") game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats" local Checkpoint = Instance.new("IntValue", leaderstats) Checkpoint.Name = "Stage" Checkpoint.Value = 0 local coins = Instance.new("IntValue", leaderstats) coins.Name = "Coins" coins.Value = 0 local rebirth = Instance.new("IntValue", leaderstats) rebirth.Name = "Rebirth" rebirth.Value = 0 --Checkpoint Section player.CharacterAdded:Connect(function(character) repeat wait() until player.character ~= nil local checkpoint = game.Workspace.Checkpoints:FindFirstChild(Checkpoint.Value) -- print("Checkpoint: "..tostring(checkpoint)) -- print("Coins: "..tostring(coins.Value)) character:WaitForChild("HumanoidRootPart").CFrame = CFrame.new(checkpoint.Position + Vector3.new(0, 2, 0)) end) --Checkpoint Section End's here --Data Saving local data local success, errormessage = pcall(function() data = myDataStore:GetAsync(player.UserId) end) if success and data then coins.Value = data.Coins Checkpoint.Value = data.Checkpoint rebirth.Value = data.Rebirth end end) game.Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() local data = { Coins = player.leaderstats.Coins.Value; Checkpoint = player.leaderstats.Stage.Value; Rebirth = player.leaderstats.Rebirth.value; } myDataStore:SetAsync(player.UserId, data) end) if success then print("Data successfully saved!") else print("There was an error saving the data.") warn(errormessage) end end) game:BindToClose(function() print("BindToClose Function For Leaderboard Runs Now!") 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 = { Coins = player.leaderstats.Coins.Value; Checkpoint = player.leaderstats.Stage.Value; Rebirth = player.leaderstats.Rebirth.value; } -- whatever you want to save myDataStore:SetAsync(player.UserId, data) end end)
And below is my Skips Script:
local DataStoreService = game:GetService("DataStoreService") local DataStore = DataStoreService:GetDataStore("SkipsSavingSystem") game.Players.PlayerAdded:Connect(function(player) local SkipStages = Instance.new("Folder", player) SkipStages.Name = "SkipStages" local Skips = Instance.new("IntValue", SkipStages) Skips.Name = "Skips" Skips.Value = 3 local data local success, errormessage = pcall(function() data = DataStore:GetAsync(player.UserId) end) if success and data then Skips.Value = data.Skips else print("Error while fetching data!") warn(errormessage) end end) game.Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() local data = { Skips = player.SkipStages.Skips.Value } DataStore:SetAsync(player.UserId, data) end) if success then print("Skips Successfully Saved!") else print("There was an error while saving Skips!") warn(errormessage) end end) game:BindToClose(function() print("BindToClose Function For Skips Runs Now!") 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 = { Skips = player.SkipStages.Skips.Value } -- whatever you want to save DataStore:SetAsync(player.UserId, data) end end)
So whenever I stop the game, I get the error "DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key"
How can I remove this error so that the data is still going to get saved properly.
And can someone also explain me how to fix this issue because I may need to save more things other than these in the future. So if I can understand how to fix this than it will help a lot.
P.S: Can you check if I have implemented the BindToClose() function properly since I recently learnt it?
Thanks for any help
That means any datastores your sending are being disregarded because your sending too many, you could get by that by making data save as a player leaves or manually save it though. And hey upvote this if you want im almost at 100 rep :)
Try wrapping them in coroutines. Do it like this:
game:BindToClose(function() for _, player in ipairs(game.Players:GetPlayers()) do -- it doesn't really matter if it's pairs or ipairs local data = { Coins = player.leaderstats.Coins.Value; Checkpoint = player.leaderstats.Stage.Value; Rebirth = player.leaderstats.Rebirth.Value; } local success, err = pcall(function() coroutine.wrap(function() DataStore:UpdateAsync(player.UserId, function(oldTable) -- i don't know why i need so many `function()` things local newTable = oldTable newTable = data return newTable end) end)(player) end) end end)
^^ for your first saving script, vv
game:BindToClose(function() for _, player in ipairs(game.Players:GetPlayers()) do local data = { Skips = player.SkipStages.Skips.Value } local success, err = pcall(function() coroutine.wrap(function() DataStore:UpdateAsync(player.UserId, function(oldTable) local newTable = oldTable newTable = data return newTable end) end)(player) end) end end)
for saving skips. Hope this helps!
When BindToClose
fires in Studio, PlayerRemoving
also fires which results in data being in queue.
First thing, add this line in BindToClose
:
local RunService = game:GetService("RunService") if RunService:IsStudio() then return end -- It will ensure that BindToClose won't fire while testing in studio
But, if you really want to test the saving in Studio:
-- You MUST comment down the PlayerRemoving function (because when BindToClose fires, PlayerRemoving also fires[ONLY IN THE CASE OF STUDIO]) --[[ game.Players.PlayerRemoving:Connect(function(plr) coroutine.resume(coroutine.create(function() DataSave(plr) -- Just an example function for saving end)) end) ]] game:BindToClose(function() local RunService = game:GetService("RunService") -- if RunService:IsStudio() then return end for _, plr in ipairs(game.Players:GetPlayers()) do -- Try using 'ipairs' as it is faster than 'pairs', but there's no 'i' in 'ipairs'. Use 'ipairs' in the case of arrays only. coroutine.resume(coroutine.create(function() DataSave(plr) end)) end end)
Lemme know if it helps!