Can't understand how to use UpdateAsync()

  1. What do you want to achieve?
    I want to understand how to use UpdateAsync() and how to save tables with UpdateAsync();

  2. What is the issue?
    I have no clue how to Update my data with UpdateAsync(), it’s just don’t saving…

  3. 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);

Thanks for help!

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)

1 Like

ok, there the code is:

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);

The issue is here:

if success then
	ValueValue = data["Value"]; --> You typed 'ValueValue' instead of 'Value.Value'
	print(plr.Name .. "'s Data has been loaded!");
else

You should give the variable ‘Value’ another name, so you don’t get confused like that.

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);

Just let me know how i can use UpdateAsync() properly?

When i was using SetAsync(), everything was fine

Did you join the game, changed the Value of the object ‘Value’, left the game, and then rejoined to check?

I already explained it in the first reply. If you still did not understand it, I recommend you search for a Tutorial on YouTube.

Didn’t found anything about UpdateAsync(), only SetAsync()

Im using table to save, so i can’t understand how to UpdateAsync() with table.

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)

Why there’s a function in SetAsync()?

SnusData:SetAsync(Key, Function(OldValue))

Nvm i got it…

but how i can save a table with it?

oh my lord, SetAsync() works pretty well, but i don’t know why UpdateAsync() doesn’t work :frowning: