I’ve considered having all player’s data auto save every minute or two lots of times, but always thought it was unnecessary.
I usually just save their data as they leave or when the server is about to close.
The fear I have is if datastores are temporarily down the moment they leave, is that why lots of developers include auto saving on top of saving as the player leaves, or is there another reason I’m unaware of?
The reason I don’t implement it is because since you can only save to the same key once every few seconds, this scenario always flashes my mind:
Auto save takes place
Within the next few seconds, the player has a change to their data and leaves
The new data isn’t saved because you can’t set the same key twice.
Once the player rejoins, they have incorrect data.
So I just wanted to know if it is necessary to auto save or not. If so, then how could I prevent said scenario from taking place?
Another reason to autosave is because when the player’s client crashes, the PlayerLeaving event doesn’t fire and the data doesn’t get saved. You shouldn’t have to autosave too often (I do it around once every 5 minutes).
Definitely, if the server crashes for whatever reason you don’t want the player to lose everything they aquired during that session.
Also save the data everytime the player decides to buy a procduct for obvious reasons.
Looking it over, correct me if I’m wrong, but the way you are implementing regular saving is by having a while wait loop, then saving the player’s data.
If that is the case, then wouldn’t the scenario I talked about above break it too, or how are you dealing with that? Or does that scenario not actually happen for some reason?
What I do is just create a player object when they join that that is set to autosave every min (as well as leaving) if one of their stats have changed. As for ensuring their data is not lost the object will hang around the server (after the player leaves) and retry saving their data every 60 seconds until success or something terrible happens.
I think its really convenient cause all I have to do push the data to the object and not have to think about it all.
When you try to set the same key twice, it just queues it and waits 6 seconds before performing the request. So this scenario would only happen if the player rejoined in less than 6 seconds. I would recommend auto saving, because you avoid the worse scenario where a player loses hours and hours of data if it fails to save when they leave. You should also save before processing purchases, so that the player can never lose purchase data.