Help With Saving Table to Datastore

UpdateAsync accepts two arguments; one is the key you want to save data to, and the second is a function that has its own parameter. This parameter contains the old data from the last previous save. What you want to do is to update your data inside this function and return the updated data.

local Data = -- your data
playerData:UpdateAsync(Key, function(OldData)
   if not OldData or OldData.Id == Data.Id then
      Data.Id = Data.Id + 1
      return Data
  else 
      return nil
  end
end)

The reason why you would want the OldData is so you don’t accidently save something you don’t want to, such as Default data received due to a GetAsync failure. You don’t seem to save “Id” or what “version” you are saving, so it’s redundant using UpdateAsync in this case, though it is certainly more effective at saving data compared to SetAsync.

I would look up how to properly use it here.

1 Like