Datastore storing tables problem?

Hi, I recently wondered if table data could be lost when updating a datastore value. If 2 people make changes at the same time, the data of the first person who saved the data in the table could be lost, due to the difference in the time of saving the data. By asking this question I want to know if my theory is correct.

example:

local DataStoreService = game:GetService("DataStoreService")

local FruitsData = DataStoreService:GetDataStore("Fruits")

game.ReplicatedStorage.AddFruit.OnServerEvent:Connect(function(fruit)
	
	local fruits = FruitsData:GetAsync("Fruits")
	table.insert(fruits, fruit)
	--some code that takes time
	FruitsData:SetAsync("Fruits", fruits)
	
end)

Yes, this is a common problem in database design. Roblox approaches this problem by providing the UpdateAsync method, which essentially acts as an atomic get/set operation.

When you call UpdateAsync, the currently-saved value is also provided, which lets you check if you ought to actually save the new value or not (you can return nil from UpdateAsync to cancel it).

It is also up to you to design your data handling in such a way to best prevent these issues from occurring. Using UpdateAsync doesn’t necessarily prevent this issue from happening, but rather gives you an opportunity to check if the currently-saved data is what you want to override or not.


Relevant paragraph in the UpdateAsync docs:

2 Likes

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