I know it has to do with the loop but I need to get all the player in order to save the stats, how do I save the stats while not getting the warning message.
game:BindToClose(function()
for i, plr in pairs(game:GetService("Players"):GetChildren()) do
local rep = game:GetService("ReplicatedStorage")
local plrStats = plr:FindFirstChild("Attributes")
local weaponTable = {}
local animationTable = {}
for i, v in pairs(plrStats.Inventory.Weapons:GetChildren()) do
if rep.Weapons:FindFirstChild(v.Name) then
table.insert(weaponTable, v.Name)
end
end
for i, v in pairs(plrStats.Inventory["Kill Animations"]:GetChildren()) do
if rep.KillAnimations:FindFirstChild(v.Name) then
table.insert(animationTable, v.Name)
end
end
local suc, err = pcall(function()
dataStore:SetAsync(plr.UserId.." Weapons", weaponTable)
dataStore:SetAsync(plr.UserId.." Animations", animationTable)
dataStore:SetAsync(plr.UserId.." Money", plrStats:FindFirstChild("Money").Value)
dataStore:SetAsync(plr.UserId.." Current Weapon", plrStats:FindFirstChild("Current Weapon").Value)
end)
end
end)
You are sending too many save requests for it to handle. It puts the other ones it cannot handle in a queue and saves them when it has taken care of the others.
The chances are that you probably also have a PlayerRemoving event which is connected to a function that also saves a player’s data. Do not worry, this warning message would not occur in a normal game. To rid this error from Studio, you can simply just use a conditional to check whether you’re on Studio in the PlayerRemoving function. Plug this conditional into the function connected to your PlayerRemoving event:
if not game:GetService("RunService"):IsStudio() then
--Code to save data, here.
end
To minimize the amount of DataStore space you’re using, try saving all player data in a single table instead of having separate scopes for each section. For example, instead of doing this:
local plr = game:GetService("Players"):GetPlayers()[1] -- Example player for this demo
local plrStats = plr:WaitForChild("leaderstats")
local dataStore = game:GetService("DataStoreService"):GetDataStore("ExampleDataStore")
local weaponTable = {"AK47", "Sniper", "Shotgun"}
local animationTable = {"Hype", "Classic", "Renegade"}
local success, errorMessage = pcall(function()
dataStore:SetAsync(plr.UserId.." Weapons", weaponTable)
dataStore:SetAsync(plr.UserId.." Animations", animationTable)
dataStore:SetAsync(plr.UserId.." Money", plrStats:FindFirstChild("Money").Value)
dataStore:SetAsync(plr.UserId.." Current Weapon", plrStats:FindFirstChild("Current Weapon").Value)
end)
if not success then warn("Could not save data for " .. tostring(plr.Name) .. "!\nError: " .. errorMessage) end
You could pack all the values into a single table and then save it like so:
local playerData = {
["animations"] = animationTable;
["weapons"] = weaponTable;
["money"] = plrStats:FindFirstChild("Money").Value;
["currentWeapon"] = plrStats:FindFirstChild("Current Weapon").Value
}
local success, errorMessage = pcall(function()
dataStore:SetAsync(plr.UserId .. "_DATA", playerData)
end)
if not success then warn("Could not save data for " .. tostring(plr.Name) .. "!\nError: " .. errorMessage) end
You’d then be causing less stress on the server as well when getting the data by only calling GetAsync once:
local playerData
local success,errorMessage = pcall(function()
playerData = dataStore:GetAsync(plr.UserId .. "_DATA")
end)
if success then
-- handle data
else
plr:Kick("Error retrieving data\n" .. errorMessage .. "\nIf you continue to receive this error, please make a bug report")
end
Can putting it in one big table ever cause it to not save everything, if that save failed?
Does it queue it up, and keep trying?
Also why is the warning sporadic ? I have a datastore in a very small dev , with only 3 leaderstats being saved, and most of the time I do not see the warning, sometimes I do.
Is it also related to what everyone is doing datastores on Roblox causing bottlenecks on their side?
Well to save it you use the dataStore:SetAsync(plr.UserId.."-data", "The thing you wanna save, for me I used the dictionary since all the stuff that I wanna save is in that dictionary.") and to load it you use the dataStore:GetAsync(plr.UserId .. "-data")
Now if everyone documented their code as fantastic as you!
Thanks!
(I might be back for more questions… off to another topic, do you know 'Mesh, export to [what] from roblox studio, the easiest app/way to put / paint / do , customs texture? . I am going to start a post.
Hm, i woundt set playerData before hand because you know…
You can handle the data inside the pcall, and then if there’s an error then get default data.