What do you want to achieve?
I want to understand how to use UpdateAsync() and how to save tables with UpdateAsync();
What is the issue?
I have no clue how to Update my data with UpdateAsync(), it’s just don’t saving…
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have been searching for information about it, but i don’t think i’ve could find answer, that i can understand one.
What i was trying to do:
Players.PlayerRemoving:Connect(function(plr)
local data = {};
data.Value = plr.leaderstats["Value"].Value;
local success, result = pcall(function()
SnusData:UpdateAsync(plr.UserId, function(oldValue)
return data;
end)
end);
if success then
print(plr.Name .. "'s Data has been saved!");
print(success);
else
warn(result);
end;
end);
UpdateAsync is used to edit existing data or save a completely different one. (Mainly for the first reason)
The function UpdateAsync is no more than a combination of GetAsync and SetAsync wrapped in just one method.
What you are doing in the code is basically the same as:
local OldValue = SnusData:GetAsync(plr.UserId) --> OldValue isn't being used at all in the script so you just wasted a Get request.
SnusData:SetAsync(plr.UserId, data)
So basically, you are using it wrong, use it to edit the existing data or if it doesn’t exist then return data like you are doing.
Other than that, I believe the issue relies on when the player joins. How are you loading the data, could you please send the code for that? (If is printing “Data has been saved!” then yeah I believe the issue is when the player joins)
local DSS = game:GetService("DataStoreService");
local SnusData = DSS:GetDataStore("SnusData");
local Players = game:GetService("Players");
Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder", plr);
leaderstats.Name = "leaderstats";
local Value = Instance.new("IntValue", leaderstats);
Value.Name = "";
Value.Value = 0;
local data;
local success, result = pcall(function()
data = SnusData:GetAsync(plr.UserId);
end);
if success then
ValueValue = data["Value"];
print(plr.Name .. "'s Data has been loaded!");
else
warn(result);
end;
end);
Players.PlayerRemoving:Connect(function(plr)
local data = {};
data.Value = plr.leaderstats["Value"].Value;
local success, result = pcall(function()
SnusData:UpdateAsync(plr.UserId, function(oldValue)
return data;
end)
end);
if success then
print(plr.Name .. "'s Data has been saved!");
print(success);
else
warn(result);
end;
end);
ok but i’ve changed it and it still doesnt save anything
local DSS = game:GetService("DataStoreService");
local SnusData = DSS:GetDataStore("SnusData");
local Players = game:GetService("Players");
Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder", plr);
leaderstats.Name = "leaderstats";
local Value = Instance.new("IntValue", leaderstats);
Value.Name = "";
Value.Value = 0;
local data;
local success, result = pcall(function()
data = SnusData:GetAsync(plr.UserId);
end);
if success then
Value.Value = data["Value"];
print(plr.Name .. "'s Data has been loaded!");
else
warn(result);
end;
end);
Players.PlayerRemoving:Connect(function(plr)
local data = {};
data.Value = plr.leaderstats["Value"].Value;
local success, result = pcall(function()
SnusData:UpdateAsync(plr.UserId, function(oldValue)
return data;
end)
end);
if success then
print(plr.Name .. "'s Data has been saved!");
print(success);
else
warn(result);
end;
end);
The same exact way as you do with SetAsync, whatever you return in the function will get saved.
UpdateAsync basically do something like this:
local function UpdateAsync(Key, Function)
local OldValue = SnusData:GetAsync(Key)
SnusData:SetAsync(Key, Function(OldValue))
end
UpdateAsync(plr.UserId, function(OldValue)
return data
end)