Game Experiences data Loss!

So, my game experiences data loss

Error:

So my data are saving good and successful, and everyone’s tho.
But some people really loose their data.

4 Likes

In cases like these you’ll need to use pcalls, these are protected calls which can run functions without erroring.
I usually do something similar to this when I get:

local invData = game:GetService("DataStoreService"):GetGlobalDataStore("Inventory")
--Getting
local didGet = false
local count = 0
local data
while not didGet do
    if count >= 1 then wait(7) end --cooldown for getting
    count = count + 1
    didGet, data = pcall(invData.GetAsync, invData, plr.UserId.."-Inventory")
end
--Saving
local didSave = false
local count = 0
local tools = {}

while not didSave do
    if count >= 1 then wait(7) end --cooldown for saving
    count = count + 1
    didSave = pcall(invData.UpdateAsync, invData, plr.UserId.."-Inventory", function(old)
	return tools
    end)
end

For more information on the limits, see here:


There’s also the great DataStore2 which caches and saves without any dataloss, you can check it out here:

5 Likes

There’s no point of using UpdateAsync if you’re not using the data that’s already saved — this is just as bad as using SetAsync. Once you’ve wrapped everything in pcalls like return suggested, I’d recommend also making sure that the data isn’t overwriting more recently saved data. For example, you could have a value in your table that goes up every time you save. When you save, you could make sure that the value of the data you retrieved is the same as the value in old. For example:

-- when loading
local tools = howeverYouEndUpGettingData() or {} -- make sure to pcall/retry like return said
if tools.Version then
	tools.Version = tools.Version + 1
end

-- when saving
howeverYouEndUpSavingThings(function(old) -- the function is used in UpdateAsync at some point
	if old and old.Version then
		if tools.Version > old.Version then
			return tools
		else
			-- your data is out of date. either don't save it,
			-- or keep track of the inventory's changes throughout the session
			-- and try to mimic the changes with the `old` data.
		end
	else
		return tools
	end
end)

Just using UpdateAsync doesn’t make your code better, you should be taking advantage of the old value it gives you. I’m sure there are much better ways to implement this, but this is just an example of a basic way you could work to prevent data loss.

This might seem unnecessary, but DataStores can be finicky. Let’s say that I join a server, do some stuff, and leave. That server might have hit the rate limits for one reason or another and need to retry a couple times before it can save the data. Then, if I join another server, make a trade really quickly, (as an example) and leave, that trade could be overwritten by the other server’s save data. This could allow a method for people to duplicate items, which is a problem with a lot of trade systems that can ruin game balance. Although this exact scenario might not apply to your game, it should be easy to see how other stuff might go wrong without being careful.

4 Likes

I found a small thread you could read through:

You can also just search ‘roblox datastore data loss’ and you’ll find many results.

1 Like