When saving data to my Datastore on my game there is like a 1/10 chance it gives me a warning about request queues and then resets my data. Its not an active game, its me testing it on studio. How would I prevent this from happening? I only send requests to save data when a player leaves or the server shuts down.
There is no problem with this, the error isn’t an actual error, but more like a warning to remind you not to save data constantly or too much.
But it resets my data. Imagine grinding your favourite game for hours and when you leave all that data is gone. Wouldn’t exactly be a fun player experience.
I don’t think this has anything to do with the warning you are getting, but more of something within your own script. I also get that warning, but my data never resets. Can we see your script, or at least the saving part of it?
Sure,
local function PlayerLeaving(Player)
DataStores.SetData(Player, {
["Survived"] = Player:WaitForChild("leaderstats",3):WaitForChild("Survived",3).Value,
["Died"] = Player:WaitForChild("leaderstats",3):WaitForChild("Died",3).Value
})
end
local function ServerClosing()
local msg = Instance.new("Message", workspace)
msg.Text = "📢 This server is closing, please rejoin!"
for i,Player in pairs(game.Players:GetPlayers()) do
PlayerLeaving(Player)
end
end
game.Players.PlayerRemoving:Connect(PlayerLeaving)
game:BindToClose(ServerClosing)
The DataStores.SetData function is my own module script shown below:
function module.SetData(Player, data)
local Datastore = module.GetDataStore()
local Success, Error = pcall(function()
return Datastore:SetAsync(module.PlayerKey..Player.UserId, data)
end)
if (not Success) then
warn("Error while getting player data:",Error)
end
end
I honestly don’t see any problems with this. Although with the WaitForChild in the PlayerLeaving, it could be waiting long enough for the Player to have already been deleted. Try printing the data within the PlayerLeaving function. (Sorry for the late reply, just ate some food)
That might be the problem. I will check when I can later in the day by replacing it with FindFirstChild instead.
I have done a little testing, and sometimes it sends 2 requests per player for each datastore (I have 2) since my PlayerLeaving function sometimes fires twice for some reason. I believe this is the root of my problem. Here are my scripts (removed unnecessary lines):
Main Script:
local function PlayerLeaving(Player)
print(Player,"leaving game") -- For debugging. Sometimes prints twice ;-;
local coins = Player:FindFirstChild("Coins") -- IntValue for player coins stored in the player
local leaderstats = Player:FindFirstChild("leaderstats")
if leaderstats then
local LeaderstatsData = {
["1"] = leaderstats:FindFirstChild("1").Value,
["2"] = leaderstats:FindFirstChild("2").Value
}
LeaderstatsDatastore.SetData(Player, LeaderstatsData)
end
if coins then
CoinsDatastore.SetData(Player, coins.Value)
end
end
-- I use the ServerClosing function more when debugging because its
-- easier to stop the server than use the normal leaving + since I'm the
-- only player in the server when I debug, it shuts down the server anyway
local function ServerClosing()
for i,Player in pairs(game.Players:GetPlayers()) do
PlayerLeaving(Player)
end
end
game.Players.PlayerRemoving:Connect(PlayerLeaving)
game:BindToClose(ServerClosing)
Module I use for DataStores (I made it):
function module.SetData(Player, data)
local Datastore = module.GetDataStore()
local Success, Error = pcall(function()
-- I added the requests thing here to track how many requests were being sent to DataStoreService. This is where I found out that my script was sending more requests than necessary when a Player leaves.
requests += 1
warn("Sending request to",Datastore," total:",requests)
return Datastore:SetAsync(module.PlayerKey..Player.UserId, data)
end)
if (not Success) then
warn("Error while getting player data:",Error)
end
end
And my console sometimes looks a little something like this:
13:03:46.063 Oliverbigman leaving game - Server - MakeWaves:65
13:03:46.064 Sending request to leaderstats_data total: 1 - Server - DataStores:57
13:03:46.064 Oliverbigman leaving game - Server - MakeWaves:65
13:03:46.064 Sending request to leaderstats_data total: 2 - Server - DataStores:57
13:03:46.239 DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = Player_66961502 - Studio
13:03:46.613 Sending request to coins_data total: 3 - Server
13:03:53.263 Sending request to coins_data total: 4