Datastore request overload, only one request

Sorry in advance if this has been answered, I’ve searched and found nothing.

I have this script that saves all the player’s stats into one table when they leave, and I get a warning telling me to try to send less requests, although I thought I was only sending one, as I am the only player in the game when it closes.

To avoid making this too long, here is the portion of the script where I save all the values into a table. The DIY in pairs loop was my attempt at fixing it, to no avail.

game:BindToClose(function()
	local players = game.Players:GetPlayers()
	for i = 1, #players do
		local v = players[1]
				local id = v.UserId
		local statstable = {v.leaderstats.Wins.Value, v.leaderstats.Kills.Value, v.Silver.Value, v.Gold.Value, v.GunTexture.Value, v.HitSound.Value}
		statstore:SetAsync(id, statstable)
		wait()
		print("Saved")
	end
end)

Also it only prints “Saved” once, and to a beginner scripter like myself that means it only sends one request, but if it actually is sending multiple requests and there is a way I can prevent it I would greatly appreciate any feedback. I’m not even sure if these warnings should be ignored, as the data still saves and loads correctly, but I figured someone on here would know and have a solution.

1 Like

If I’m understanding correctly, this is a way to save all player data when the server closes, or is your intention to just save one person’s data? The reason I’m asking is because you might want to define local v as players[i], becuase if you have more than 1 person at the time of the server closing, then they will all be set as the first player’s userid, which could cause a lot of data loss for the other players. Or, maybe I’m wrong, but I just wanted to point that out.

Now, as for your question, do you also save the data through a PlayerRemoving function?

You need add wait((make sure to have number to prevent infinite yield)) function before saving data (SetAsync), The loop is repeatly loop again on same player, I think.

Yes I do save the data through a PlayerRemoving function, and thanks for catching that players[i] error I didn’t realize.

With that playerremoving function, you should not need that saving when the server closes. What I see bind to close used for in datastores is just to delay the amount of time till the server shuts down, so that people’s data can be saved.

But the reason you are getting too many requests is because as soon as it sets the data with the playerremoving function, the bind to close function also saves it (or at least tries to.)

And to add on, the reason the data is still being saved even with the warning is because the playerremoving function saves it, but the bind to close can’t due to the amount of requests.

1 Like

I just removed the bind to close function and tested it a few times with no warn! For now I’m gonna say that this was the reason, so thanks for your feedback!

1 Like