Broken UpdateAsync() DataStore script

This stupid dumb data saving script won’t work. I don’t even know what is the issue, it loaded the data correctly, it did do the correct thing when the player does not have any previous data, which will print no prev data, but it just doesn’t update the data. The pcall said it is a success to update the data, BUT when it loads back the data, it just say the data is nil like it never existed. PLEASE HELP.

This script saves the data.

local function SavePlayerData(player)
	local key = player.UserId
	
	local PurchasedTools = {}
	
	-- saving purchases tools
	for index,tool in pairs(player:FindFirstChild("PurchasedToolsFolder"):GetChildren()) do
		table.insert(PurchasedTools,tool.Name)
	end
	
	local success, result = pcall(function()
		DataStore:UpdateAsync(key,function(oldData)
			local currentData = {
				["Coins"] = player.leaderstats:FindFirstChild("Coins");
				["PurchasedTools"] = PurchasedTools;
				["DataVersion"] = player.DataVersion.Value
			}
			local previousData = currentData.DataVersion
			if oldData == nil then
				print("no prev data")
				-- data is safe to save
				print(currentData)
				return {
					["Coins"] = player.leaderstats:FindFirstChild("Coins");
					["PurchasedTools"] = PurchasedTools;
					["DataVersion"] = (previousData + 1)
				}
			elseif oldData.DataVersion == currentData.DataVersion then
				print("same version")
				-- data is safe to save
				return {
					["Coins"] = player.leaderstats:FindFirstChild("Coins");
					["PurchasedTools"] = PurchasedTools;
					["DataVersion"] = (previousData + 1)
				}
			
			else
				warn("WARNING: "..player.Name.."'s data could not be saved due to different data versions.")
				return nil
			end
		end)
	end)
	
	if success then
		print("DATA SAVED FOR "..player.Name..".")
	else
		wait("DATA FAILED TO SAVED FOR "..player.Name..". ERROR: "..result)
	end
end

This is the script that loads my data.

local function GetPlayerData(player)
	local key = player.UserId
	
	local success, result = pcall(function()
		return DataStore:GetAsync(key)
	end)
	
	if success then
		print(result)
		if result ~= nil then
			print("Has data!")
			for data, value in pairs(result) do
				print(data)
				if data == "DataVersion" then
					local DataVersion = Instance.new("IntValue")
					DataVersion.Name = data
					DataVersion.Value = value
					DataVersion.Parent = player

				elseif data ~= "PurchasedTools" then -- leaderstats data
					player.leaderstats[data].Value = value

				elseif data == "PurchasedTools" then -- purchased tools
					for _, toolName in pairs(value) do
						local ToolTag = Instance.new("BoolValue")
						ToolTag.Name = toolName
						ToolTag.Parent = player:FindFirstChild("PurchasedToolsFolder")
					end
				end
			end
		else
			-- new player
			print("No data.")
			for data, value in pairs(DefaultData) do
				print(data)
				if data == "DataVersion" then
					local DataVersion = Instance.new("IntValue")
					DataVersion.Name = data
					DataVersion.Value = value
					DataVersion.Parent = player

				elseif data ~= "PurchasedTools" then -- leaderstats data
					player.leaderstats[data].Value = value

				elseif data == "PurchasedTools" then -- purchased tools
					for _, toolName in pairs(value) do
						local ToolTag = Instance.new("BoolValue")
						ToolTag.Name = toolName
						ToolTag.Parent = player:FindFirstChild("PurchasedToolsFolder")
					end
				end
			end
		end
	end
end

Why are you saving Purchased tools in a table when its already in a table?, your using :GetChildren() which returned a table and your inserting it into another table. Just do:

local Purchased = Player:FindFirstChild("PurchasedToolsFolder"):GetChildren()

This doesn’t help my problem, also, this is how I used to load the data in my game. This has nothing to do to fix my problem.

Isn’t this supposed to be Coins.Value then?

Whoops, although is still isn’t anything to do with the data store, thanks for finding this mistake.

Is currentdata a variable?, and where is it declared?

CurrentData is under the scope of UpdateAsync() function. Let me update my post a little,

You used “wait(“DATA SAVE ERROR”)” instead of warn(). You should fix it too.

Try setting CurrentData to the return data table here:

local Table = {
	["Coins"] = player.leaderstats:FindFirstChild("Coins");
	["PurchasedTools"] = PurchasedTools;
	["DataVersion"] = (previousData + 1)
}

CurrentData = Table

return CurrentData

And does it warn anything?

Ah sorry, thanks for correction but still not the cause here.

@ThoseNamesAreGood I don’t thing that works as well, tried that but didn;t work. But I will try to do this again if I can.

1 Like

Thanks to my good friend, @WaterJamesPlough , he managed to help me fixed it by returning the .Value properties, or make the pcalls() shorter. I really don’t know which one fixed it.