How to prevent data from saving twice?

I have a data save script, that fires whenever a player is removed and when the server shuts down

game.Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
	for i, v in pairs(game.Players:GetPlayers()) do
		saveData(v)
	end
end)

The problem with this, is that when the last player in the server leave, both functions fire, and data will save twice. The problem with that is there will be this warning:

DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 1272029006-inventory

So how do I ensure the data is not saved twice? thanks a lot

1 Like

I would assume you don’t need to add the bind to close function because when the server shuts down, all the players leave anyways.

there are a lot of things to save, so the server might shut down before everything can be saved. Bindtoclose is to prevent that

You could try checking how many people are in the server and if theres only 1 player in the server don’t save the data when the server closes

If your reaching your limit I don’t think it is caused from that double saved, though you should fix that by checking how many players are there. But you should lower your requests, if you don’t really know how profile service deals with a lot of these issues!

The problem is with server shutting down. There are more than 1 player so BindToClose is needed

How often does the data save? If it’s quite often then maybe try lowering it.

Only twice. Once for player removed function and once for bind to close (if player is last in the server

I actually am having this same issue too, both of those functions tend to run when the game closes, so which one fires first? If we can figure that out, then maybe there’s a fix.

What about whilst the game is running? How many request are being sent?

None are sent

more text here cause thirhty characters

I’m not really sure how you could lower the request, unfortunately. I do recommend using ProfileService or Datastore2 since it I believe they make it really easy to save data and I haven’t run into any issues with the too many requests. The only thing I could think of is adding the unsaved data to a table and maybe trying to save it a couple of seconds later however not sure how this would work in a bind to close.

You should compare your code to mine, or learn from mine.

(Summary → Final Product)
I hope this helps you! :slight_smile:

1 Like

To lower the request, I just need to prevent data from saving twice

Thanks, it worked, but how is task.spawn solving the problem?

It creates a new thread, aka it won’t yield or wait for the next person to be done saving their data.
This can be helpful for stopping dataloss as it won’t take as long.

But how does it solve the double saving problem? you’re still having the 2 events (player removing and bind to close), and both should fire, but it doesn’t anymore.

in the guide you posted you had written the same thing

*I have no problem with data loss. I just don’t want the PlayerRemoving Event and BindToClose to fire together.

That issue only pertains to studio as the server is hosted locally, you don’t need to worry about it in a live session.

if run:IsStudio() then return end

You can always use the above line of code too (run is a reference to the RunService).

1 Like