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

2 comments
So am I supposed to remove the BindToClose() function?
no, check out my answer