Can someone tell me what's wrong with this data saving code?

local success, errorMessage = pcall(function()
	MainDataStore:UpdateAsync(player.UserId,function(oldValue)
		local previousData = oldValue or {DataId = 0}
		if player.DataId.Value == previousData.DataId then
			data.DataId += 1
			warn("Saved data 1")
			return data
		else
			if previousData.DataId == nil then
				data.DataId += 1
				warn("Saved data 2")
				return data
			else
				warn("Saved no data 1")
				return nil
			end
		end
	end)
end)

When people rejoin my game sometimes, they don’t get their newer data, but instead get their older data

Edit: does this work too?

local success, errorMessage = pcall(function()
	MainDataStore:UpdateAsync(player.UserId,function()
		data.DataId += 1
		return data
	end)
end)

SetAsync is preferable than UpdateAsync when it comes to tables since the latter is essentially redundant.

2 Likes

data loss happens when you reach more than 200 players with SetAsync, and my game has like 1k average

Use a DataStore to store your single values and then you can update each value.

1 Like

why do I want to update these values though? Why can’t I just like, save it to the data store with no hassle?

You’d have to use SetAsync then. UpdateAsync on a table is pretty much doing the same thing as SetAsync with a table. DataStore does not account for tables when using UpdateAsync.

1 Like

SetAsync causes data loss. Tons of posts say that. If you owned a game with more than 200 players you’d know. One day the data loss strikes and it feels like the end of the world