Data store help

Can someone just give me example code for an UpdateAsync function saving data? I understand how the data id system works and stuff, but on my game I’m having issues and reports of their data not saving, but loading successfully.

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
return data
else
if previousData.DataId == nil then
data.DataId += 1
return data
else
return nil
end
end
end)
end

Dont use update async , stick to setasync. Also is there a need to check for previous data in the dss stuff, try checking before hand and seperating out backups from another section

I made a script with backup checking, basically after 10 attempts it checks for previous data, it also checks if the data has become nil meaning corrupted data

Also you can post scripts with format using 3 ’

1 Like

As minimic2002 said, UpdateAsync shouldn’t be used in this situation. At least, I wouldn’t. UpdateAsync is a safe way to update already existing data. It is used when more servers update the same DataStore so the saved data doesn’t overwrite. In your case, I assume there is only one server changing the saved data. That’s why I would use SetAsync as well.

UpdateAsync also needs an initial value as I know.

2 Likes

I see nobody here has a game with more than 200 players. Well, what Roblox does is they give you data loss once you hit 200 players and above. I HAVE to use UpdateAsync, or else 1/5 players will get data loss every time they leave.