Using UpdateAsync to save tables isn't working

So, I decided to use UpdateAsync to save and update data. But I am having a problem doing both, my script doesn’t save or update data.

I switched to UpdateAsync only because when I would use SetAsync would override other tables I had saved.

I tried searching how to properly save tables using UpdateAsync but I can’t seem to find any right ways, all the ones I saw don’t explain they just give the scripts out.

UpdateAsync Script
local success, errormessage = pcall(function()
		myDataStore:UpdateAsync(key,function(oldData)

			local newData = oldData or SavedItemIDs

			for i, item in pairs(ItemsFolder:GetChildren()) do --Items
				newData.x[item.Name] = item.Value
			end

			return newData

		end)
	end)
What x is being set to
if player:FindFirstChild("SlotUS") then --If the player has "SlotUS" in it, then the value of x will equal Outfit1
		x = "Outfit1"
	elseif player:FindFirstChild("SlotNVA") then --If the player has "SlotNVA" in it, then the value of x will equal Outfit2
		x = "Outfit2"
	elseif player:FindFirstChild("SlotALLIED") then --If the player has "SlotALLIED" in it, then the value of x will equal Outfit3
		x = "Outfit3"
	end
What SavedItemsIDs is
--NoDataUS, NoDataNVA, NoDataAL are preset outfits for players that have no data saved
local SavedItemIDs = {
	["Outfit1"] = {},
	["Outfit2"] = {},
	["Outfit3"] = {},
	["NoDataUS"] = {["M1C"] = "hat",["Pants"] = "http://www.roblox.com/asset/?id=5053577948",["Shirt"] = "http://www.roblox.com/asset/?id=5054465259",["Trecky1"] = "hair",["Web"] = "web",["face"] = "http://www.roblox.com/asset/?id=1942392454",["skin"] = "Pastel brown"},
	["NoDataNVA"] = {["M1"] = "hat",["Pants"] = "http://www.roblox.com/asset/?id=5053577948",["Shirt"] = "http://www.roblox.com/asset/?id=5054376077",["Trecky1"] = "hair",["Web"] = "web",["face"] = "http://www.roblox.com/asset/?id=343187883",["skin"] = "Nougat"},
	["NoDataAL"] = {["M1C"] = "hat",["Pants"] = "http://www.roblox.com/asset/?id=4831959204",["Shirt"] = "http://www.roblox.com/asset/?id=4831982288",["Trecky1"] = "hair",["Web1"] = "web",["face"] = "http://www.roblox.com/asset/?id=343187883",["skin"] = "Pastel brown"}
}

btw this is under player removing.

Idk if its your issue, but you could try using JSON Encode / Decode. It basically converts a table into a compressed string to be saved.

--saving
	local savestring = HTTPS:JSONEncode(DataTable)
	LogStore:SetAsync(player.UserId, savestring)

--loading
 local DS= DataStore:GetAsync(player.UserId)
 local DataTable
 if DS then
	 DataTable = HTTPS:JSONDecode(DS)
 else
	 --No data exists
 end

I’m trying to step away from using SetAsync. And I already tried that, no results.

You’re using the string “x” here, not the variable x. table.y will use the string, which is why bracket notation exists.

1 Like

That’s not true at all. GetAsync will never overwrite anything.

2 Likes

You said GetAsync.

1 Like

I did say GetAsync. Your point is?

That sounds like bad error/edge case handling, not the fault of GetAsync. GetAsync won’t just “return nil” when it fails, it will fail.

1 Like

This is not true. Get async does not set anything in a datastore. It simply returns something.

1 Like

Which is of course used to return the player’s data (in most cases.) UpdateAsync is the easier, and safer way to handle DataStore failures. Which is why I would avoid GetAsync()

I put it used brackets and I still got nothing - newData[x][item.Name]

:confused:
Again. GetAsync() is ok for people to use. SetAsync() is not ok for people to use.

1 Like

It’s okay but not great.

It’s probably not a risk worth taking. Atleast, to me it isn’t. This has gone way off topic. I’ll continue using UpdateAsync, and you can continue with whatever you like to use.

1 Like

I solved this problem by adding a :BindToClose() function. Reason why I didn’t add it in the first place was because when I was using :SetAsync(), PlayerRemoving ran all the time and I had no issues of data not saving, loading, and etc.