How to use UpdateAsync with tables (datastoreservice)

I have been with my game but I have a problem yesterday I tried it in the studio and I had a data loss due to a bad internet and it was saved but I did not load what was saved, those who do know will realize that SetAsync saves by force. and GetAsync obtains the data but it has a time limit and if in that time it does not load due to a bad connection it does not load and SetAsync takes action saving an invalid value (a lost value)

Here I will leave you a script which I do not ask you to modify but rather tell me how I should use Pcall funtion etc… The point is that they promote me in case you are a very noble person without a doubt I would accept the modified script:

function saveData(player)

	local charactersList = {}

	for i, character in pairs(player.OwnedCharacters:GetChildren()) do
		table.insert(charactersList, character.Name)
	end
	
	
	ds:SetAsync(player.UserId .. "Characters", charactersList )
	
	
		game.ReplicatedStorage.OriginalCharacters[player.Name]:Destroy()
		
end

In summary I want the server to detect if the folder has no children and if it does not have children then it should not save it (using updateAsync) the folder is: “OwnedCharacters” you can see it in the for loop part
I got the script from a youtuber but I modified it to my style it cost me a lot hahaha
if you have doubts ASK ASK ASK PLEASE WITHOUT FEAR WHEN YOU DON’T KNOW SOMETHING PLEASE ASK

1 Like

You can use the pcall function to wrap around your code to catch any errors that might occur. The pcall function takes a function as an argument, and if an error occurs in that function, it returns false along with the error message, rather than halting the program and throwing an error. Here’s an example of how you can use pcall to wrap the code in your saveData function:

function saveData(player)

	local charactersList = {}

	for i, character in pairs(player.OwnedCharacters:GetChildren()) do
		table.insert(charactersList, character.Name)
	end

	local success, errorMessage = pcall(function()
		ds:SetAsync(player.UserId .. "Characters", charactersList )
	end)
	
	if not success then
		warn(errorMessage)
		return
	end

	game.ReplicatedStorage.OriginalCharacters[player.Name]:Destroy()

end

In this example, the call to ds:SetAsync is wrapped inside a function passed to pcall . If an error occurs in that function, the success variable will be set to false, and the error message will be stored in the errorMessage variable. In this case, if success is false, the function will display the error message using warn and then return without executing the code that destroys the OriginalCharacters object.

To prevent data loss due to a bad internet connection, you can consider using the updateAsync function instead of SetAsync . The updateAsync function will update the value only if it has not been changed since the last time you obtained it. Here’s an example:

function saveData(player)

	local charactersList = {}

	for i, character in pairs(player.OwnedCharacters:GetChildren()) do
		table.insert(charactersList, character.Name)
	end

	local success, errorMessage = pcall(function()
		ds:UpdateAsync(player.UserId .. "Characters", function(oldValue)
			if #charactersList > 0 then
				return charactersList
			else
				return oldValue
			end
		end)
	end)

	if not success then
		warn(errorMessage)
		return
	end

	game.ReplicatedStorage.OriginalCharacters[player.Name]:Destroy()

end

In this example, the updateAsync function takes a function as an argument that determines the new value to be stored. The function checks if charactersList has any elements. If it does, it returns charactersList as the new value to be stored. If it does not, it returns the old value, which was obtained using updateAsync .

1 Like

I have not tried it but it is very useful and obvious if it works the truth I thank you very much you are an intelligent girl

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.