Some saving issues, trying to make the game run better

Greetings,

I have a ton of values to save (since the game I am making is based a lot on numbers), and instead of saving them whenever they would change (that was creating a ton of lag) or making a manual button to save them (which many people would forget about or would not even notice it). Instead, I wanted to make the values save when the player has left the game. The problem is that my script is not saving it. Here is the problem with the script (none other parts of my scripts matter by the way):

game.Players.PlayerRemoving:Connect(function(plr)
	print("Player left, saving all values!")
	Money:SetAsync(plr.UserId,plr)
	TotalMoney:SetAsync(plr.UserId,plr)
	OrderMoney:SetAsync(plr.UserId,plr)
end)

Please help me to make this system work. Thank you!

It might be that server closes before saving the data, since it closes after all of the players left, try playing it with multiple players and see if the ones that left first have their data saved. You can tell the server to wait after all players have left though:
https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

1 Like

Thanks for your quick answer. Sadly, the game has a one-player server only because it doesn’t require more, and it is supposed to be a very fast game where there should be no lag or other player interaction (more of a speedrun to be exact). I thought about using a save mechanism every 60 seconds, but that just creates a ton of lag when trying to get good timing on completing difficult achievements.

Usually it is best to save when player is leaving. You just have to make sure server doesnt shut down before data is saved so BindToClose will help.

1 Like

Alright. Sorry if I ask by the way, but can you please show me how to use that? I don’t know how to. I have never used that function…

The link I provided above has an example for saving players data, I believe you could just use that and change some things to save all of the data in your game. The function gets activated when server starts closing so you could just put it at the end of the datastore script and it should work.
Alternatively I believe you could just have task.wait(30) in the BindToClose function and keep the datasaving in the function of playerleaving.

1 Like