I watched tons of datastore videos and see NOTHING wrong with my code
local DSService = game:GetService("DataStoreService"):GetDataStore("Currency") game:GetService("Players").PlayerAdded:connect(function(plr) local leader = Instance.new("Folder",plr) leader.Name = "leaderstats" local Credits = Instance.new("IntValue",leader) Credits.Name = "Credits" local Diamonds = Instance.new("IntValue",leader) Diamonds.Name = "Diamonds" local key = "player-"..plr.UserId local savedData = DSService:GetAsync(key) if savedData then Credits.Value = savedData[1] Diamonds.Value = savedData[2] else local values = {Credits.Value, Diamonds.Value} DSService:SetAsync(key,values) end end) game:GetService("Players").PlayerRemoving:connect(function(plr) local ValuesToSave = {plr.leaderstats.Credits.Value, plr.leaderstats.Diamonds.Value} DSService:SetAsync(plr.UserId, ValuesToSave) end) while wait(300) do for i,v in pairs(game:GetService("Players"):GetChildren()) do local key = v.UserId DSService:SetAsync(key,v.leaderstats.Credits.Value,v.leaderstats.Diamonds.Value) end end
First, the GetAsync
part is really messed up. The arguments are completely messed up. The correct arguments are:
GetAsync(plr.UserId .. "-NameOfTheScopeHere")
Also, do not use variable to replace the plr.UserId, it'll mess it up. You should use:
local savedData = DSService:GetAsync(plr.UserId .. "-WhateverNameYouWant")
Second, the SetAsync
are also really messed up. Once again, the arguments are wrong. The correct arguments should be:
SetAsync(plr.UserId.. "-TheNameShouldBeSameWithTheUpperOneOrDataWillNotLoad", ValueToSave)
In that way your data saving problem should be gone forever.
A little tip:
Consider using UpdateAsync
, because if you use too much SetAsync, the DataStoreService will overwhelm. SetAsync and UpdateAsync have a different budget. They don't share the same budget. In that way, it's less likely to have data loss reports.