Ok so DSQueue is a table where i put the data that needs to be saved
function SaveDataWhile (player)
DSWaitTime = 60
local inventory = player:WaitForChild("inventory",10)
local LoadingVal = inventory:WaitForChild("Loading",10)
if LoadingVal.Value == false then
local playerId = "Player_"..player.UserId
local leaderstats
local dataShavings
local dataWins
local TotalCharactersStatData
local succes, errorMsg = pcall(function ()
leaderstats = player:WaitForChild("leaderstats")
dataShavings = leaderstats:WaitForChild("Shavings").Value
dataWins = leaderstats:WaitForChild("Wins").Value
TotalCharactersStatData = inventory.missions.stats.TotalCharactersStat.Value
end)
if not succes then
warn(errorMsg)
end
local success, errorMsg = pcall(function()
ShavingsData:SetAsync(playerId, dataShavings)
WinsData:SetAsync(playerId, dataWins)
end)
if success then
print("Shavings "..playerId.."-"..dataShavings)
print("Wins "..playerId.."-"..dataWins)
else
print(player.Name.."'s shavings and wins couldnt been saved")
warn(errorMsg)
end
end
local an = 0
for i, DSQueuePart in pairs(DSQueue) do
print(DSQueuePart["name"])
local succ, err = pcall(function ()
DSQueuePart["data"]:SetAsync(DSQueuePart["id"], DSQueuePart["val"])
end)
if succ then
print(player.Name.."'s "..DSQueuePart["name"].." Data was saved.")
else
warn(err)
end
table.remove(DSQueue,i)
end
end
Yeah you use SetAsync 3 times, eventually that will hit a mark. I can show you a piece of code of what I use, I could improve the UpdateAsync function + the script overall, but its just an example. ( a piece became my whole script)
local dataStoreService = game:GetService("DataStoreService")
local key = "Test1"
local ds = dataStoreService:GetDataStore(key)
local stats = {
["leaderstats"] = {
["Stage"] = 0;
}
}
game:GetService("Players").PlayerAdded:Connect(function(plr)
local data = ds:GetAsync(key.."/"..plr.UserId)
print(data)
for i, StatTable in pairs(stats) do
local folder = Instance.new("Folder")
folder.Name = i
folder.Parent = plr
for stat, value in pairs(StatTable) do
local val_new = Instance.new("NumberValue")
val_new.Name = stat
val_new.Value = value
val_new.Parent = folder
if data then
val_new.Value = data[folder.Name][stat]
end
end
end
plr.CharacterAdded:Connect(function(char)
local humanoid,hrp = char:WaitForChild("Humanoid"),char:WaitForChild("HumanoidRootPart")
task.wait()
if humanoid and hrp then
local part = workspace.Stages:FindFirstChild(plr.leaderstats["Stage"].Value)
hrp.CFrame = part.CFrame + Vector3.new(0,1,0)
end
end)
end)
local function SaveData(plr)
local tableToSave = stats
for i, folder in pairs(plr:GetChildren()) do
if folder.ClassName == "Folder" then
for i, stat in pairs(folder:GetChildren()) do
tableToSave[folder.Name][stat.Name] = stat.Value
end
end
end
if tableToSave then
local tries = 0
repeat
tries += 1
local succ, err = pcall(function()
local data = ds:UpdateAsync(key..'/'..plr.UserId,function(olddata)
return tableToSave
end)
end)
if err then warn(err) task.wait(6) end
until succ or tries == 3
print("Data saved for " .. plr.Name)
end
end
game:GetService("Players").PlayerRemoving:Connect(function(plr)
SaveData(plr)
end)
game:BindToClose(function()
for _, plr in ipairs(game:GetService("Players"):GetPlayers()) do
coroutine.wrap(SaveData)(plr)
end
end)
What scares me is I see success or tries == 3… what if tries equals 3 without success, and how does this really work, since it can just be luck. But if a game has more than 200 players, SetAsync will fail, and tons of player data will get wiped.