What is wrong with my saving system? Players find they lose data frequently

I get about 20 complaints a day over players losing data on my game. I have tried fixing the saving system countless times but I really can’t figure out why data is being lost, which is why it seems almost impossible to fix - as I have no idea where to start fixing.

The script is a bit long so I may cut bits out, which are bits where information is being processed rather than saved.

Here is the saving function:

Attempt load function:


Here are the main bits of code that incorporate the functions:
image

I know it’s mega messy, this is just because I’ve had to tweak it so many times and it’s slowly become a mess.
I also have an autosave script which saves data in regular intervals for a certain number of players.

The script looks like this:
image

The saving system (or loading possibly) is the biggest issue with the game, causing hundreds of complaints, and forcing me to have to go into games with these people and redeem their items or stats manually - which takes a very long time.

Just wondering if I’m doing anything wrong, because it seems to only work about 90% of the time.

Your autosave loop doesn’t check if the data can be saved, like your PlayerRemoving call does.

If a player joins and their data fails to load, it would set default data, then if they wait long enough for the loop to be triggered for them, the default data would be saved over their actual data.

Moreover, I would highly recommend just keeping your interval in your autosave loop simple:

while wait(60) do
   for i,v in pairs(game.Players:GetPlayers()) do
      local info = workspace.Player_Information:FindFirstChild(player.Name)
      if info and info.Can_Save.Value then
         save_data(v)
      end
   end
end

I hope this gets the idea across.

(Make sure to check for mistakes in that code, I wrote it in the editor here so I didn’t check it for correctness or anything.)


Also, I hope Can_Save is defaulted to false when you first create it. You should set it to true when data succeeds loading. Not just set it to false when data fails to load, because that is subject to time sensitive conditions (i.e. what happens if the player leaves before Can_Save is set to false? Oops!)

3 Likes

bind your save function to server shutdown, it looks like when the server is shutting down (aka last player leaves) the data isn’t saving in time