Datastores reverting to older versions of data

This is my first time posting here so sorry for any weird typos or anything in the post.

I’ve been dealing with an issue for a while where players data will save and randomly revert to an older version of their data a couple of servers later.

People have been saying that they will buy something ingame, and around an hour or two later their data reverts to an older version of their data.

The datastore script saves their data in a table whenever they leave and retrieves it when they join

local function Retrv()
   Key = DataStore:GetAsync(ID)
   if Key then
      local Suc, Err = pcall(function() 
       for _, Targ in pairs(Inv:GetDescendants()) do if Targ:GetAttribute("ObjName") == nil then if Targ:IsA("StringValue") or Targ:IsA("NumberValue") or Targ:IsA("BoolValue") then 
               if Key[Targ.Name] and Targ:GetAttribute("ObjName") == nil then           
                  Targ.Value = Key[Targ.Name]
               end         
            end
         end end
      end)
      if Err then Player:Kick("Data failed to load.") end
      if Suc then GotData = true end    
   else
      Values = {}
      for i, v in pairs(Inv:GetDescendants()) do if v:IsA("StringValue") or v:IsA("NumberValue") or v:IsA("BoolValue") then   
            Values[v.Name] = v.Value
      end end
      DataStore:SetAsync(id,Values)
      GotData = true
   end 
   end

And the data saving function is

local function SAVELEAVE()

      local VValues = {}

      if Inv and Inv:FindFirstChild("Kaiju") then
      for i, v in pairs(Inv:GetDescendants()) do if v:IsA("StringValue") or v:IsA("NumberValue") or v:IsA("BoolValue") then
         if v and v:GetAttribute("ObjName") == nil and v.Value then
            VValues[v.Name] = v.Value
         end
      end end 
     
      DataStore:SetAsync(ID,VValues)

      repeat
      task.wait()
      local Succ,Err = pcall(function()
      DataStore:SetAsync(ID,VValues)
      end) if Succ then Success = true end
      until Success == true
      print("saved")
      end

   end

I’ve been trying on my own for a long time to try and fix this but I have no clue what causes their data to revert to an older version, and auto saving+saving when they leave doesn’t fix it

In your case here, there is a very strong dependency on all these objects within these folders. Apart from that, you could try:

  1. Migrating to Data objects for each player’s data
    I would suggest moving your data to a Server Script which has tables for every player that joins, tracking their data. I have had past experiences where my folder objects delete for god knows what reason, and that causes many errors.

  2. Applying the DataStore2 module to your game
    This modules is incredibly useful in guarenteeing gradual saves of a player’s data. It has helped me a ton with data loss, and there are loads of video tutorials for beginners.

  3. You can make a logging system for everytime someone’s data errors
    Too many times, developers cannot figure out the situation. Players of your game as well do not have access to Server logs, meaning they cannot screenshot you what is going on. Try to save each error message to a DataStore Table and come back to it time by time to see if maybe it is something within your table.

Let me know if this makes sense.

I recommend to not use the default Data store service (especially if your game is a bigger games with higher counts of people) It’s not a great service, for data saving and there is a lot of better ways to save data. I use the profile service which is made by devs, That have spent a long, long time for saving data. This is what I recommend, and Below I have a video (made by MonzterDEV) that’s made on the profile service & it explains it well. Profile Service Video so this may work better for you.