How to stop datastore erasing data if it's not updated between places

The title is confusing, basically, the LoadData function will attempt to write data to values inside of a folder. The thing is, the game uses places, and certain data is not useful in place one, so i don’t want to bother to add the value there too. I tried using UpdateAsync but i didn’t understand how it worked originally. anyways, here’s the code (kinda messy)

local DataStore = game:GetService("DataStoreService"):GetDataStore("Encore")

local CanSave = false

local function SaveData(plr)
	if CanSave then
		local Data = {}
		for _, Values in pairs(workspace.Technical.DataValues:GetDescendants()) do
			Data[Values.Name] = Values.Value
			print(Values.Value)
		end
		print("save:", Data)
		DataStore:SetAsync(plr.UserId, Data)
	end
end

local function LoadData(plr)
	return DataStore:GetAsync(plr.UserId)
end

--//loading data

game.Players.PlayerAdded:Connect(function(plr)
	local success, data = pcall(function()
		return LoadData(plr)
	end)	
	print(success)
	print("load:", data)
	if not success then
		CanSave = false
		local tries = 0
		while task.wait() do
			if tries <= 10 then
				plr:Kick("All attempts to load data were unsuccessful. Please rejoin or try again later.")
				break
			else
				tries += 1
				success, data = pcall(function()
					return LoadData(plr)
				end)
				if success then
					CanSave = true
					print("Loaded")
					break
				end
			end
		end
	else
		CanSave = true
	end
	if not data then
		data = {}
	else
		for _, val in pairs(workspace.Technical.DataValues:GetDescendants()) do
			local succ, err = pcall(function()
				val.Value = data[val.Name]
				print(val.Value)
			end)
			if not succ then
				warn("Key not found or mismatched values. Luau error:", err)
			end
		end
	end
end)

--//saving data

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormsg = pcall(function()
		SaveData(plr)
	end)

end)

game:BindToClose(function()
	for _, plr in pairs(game.Players:GetPlayers()) do
		local success, errormsg = pcall(function()
			SaveData(plr)
		end)
	end
end)

--task.spawn(function()
--	while task.wait() do
--		print(CanSave)
--	end
--end)
1 Like

IG you can save everything as a dictionary
e.g.

{
    ["Money"] = 150,
    ["XP"] = 51,
    ["Swords"] = {"Bob", "Gary", "Sir Antelope the Great"}
}

Then, when you are loading the data you access the keys that you need.

To save the data, you would use UpdateAsync, in some way like this:

DataStore:UpdateAsync(plr.UserId, function(old)
    old["Money"] = 50
    return old
end)

the above code would make the value of Money be 50, while leaving the other values untouched.

1 Like

Not really what I asked for, but you made me understand updateasync completely, and I’ve come up with a solution while I was gone. I’ll just use table.find to compare the tables and if a value is missing, insert it.

I fixed it. I switched to update async, checked if the old data is nil, the looped through the old data and tried to find keys comparing the old data and new data via table.find(OldData, index), and if it’s nil then it will add the index and the value, and at the end (outside the if check for the old data) I returned the new data to save. I don’t have the code on me unfortunately.aaa

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