Making Auto-Save

Hello everyone,

Besides saving stats when a player is leaving, I am also trying to make an auto-save script which saves each player’s stats to a datastore every 5 minutes.

This all works fine but whenever a player is leaving the game while the game is auto-saving I get an alert message saying:

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

After reading this article: Documentation - Roblox Creator Hub I found out that a Set/UpdateAsync has a cooldown of 6 seconds. Which explains why I am getting this alert message.

This problem could be fixed using GetRequestBudgetForRequestType

I have read this article explaining how it works but I still don’t understant how to implement this.

Can someone explain me how to use this properly? So that when a player is leaving a server while an auto-save is taking place, no alerts will pop up and that their data is stored?

DataStoreService:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.UpdateAsync)

Will return the number of requests that can be made for the given request type, in the code above, it will return the number of requests that can be made for Update Async, if this returns 0, the function will return the error you posted when you attempt to use it, the same applies for Set Async.

I do not use GetBudget, but a possible use would be

if DataStoreService:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.UpdateAsync) > 0 then
    local Success, Result = pcall(--Data Store Method)
end
2 Likes

Alright, thank you.

Do you prefer or know a better way to prevent this problem from occurring?

What I do for auto saving is save the data to the server inside a table, not the data store right away, it gives off the impression that the data is saved, but is actually cached on the server. The only time the data will be actually saved is when the player leaves the game.

4 Likes

Set/UpdateAsync doesn’t have a cooldown of 6 seconds, using a write request (Set, Update, Increment and Remove) collectively have a 6 second cooldown if you’re writing to the same key.

The easiest way you can handle this is by setting up an intermission-based system in your data handler. Whenever a write request is made, log it in a table and prevent any write requests to the key for 6 seconds. You can also, optionally, include a retry function that attempts to write to the key again after the cooldown is reached or the request has enough budget to be made.

2 Likes

So a possible solution could be that I make a value which I store in the server, let’s say an integer, and whenever an auto-save is happening, this value goes to 1 (lets suppose the default value is 0). And after 6-7 seconds of the auto-save, this value goes back to 0.

So when a player is leaving the server,
the server first checks wether the value is 0 (if not, it waits untill it becomes 0), and then saves the data?

You could just use a table. Include all related data management code in one script (loading, saving, autosaving, etc).

local saveBlock = {}

local function performWriteRequest(key) -- Example
    if not saveBlock[key] then
        doSomeSaveThing()
        saveBlock[key] = true
        delay(6, function ()
            saveBlock[key] = false
        end)
    end
end

Obviously how this gets handled varies from project to project. I wouldn’t use my code raw since it’s an example, but hopefully it gives you an idea of what to look for.

3 Likes

Thanks!

But what if a player buys something within 6 seconds after an auto-save is made?
the saveBlock[key] would still be true so no new data would be saved?

Nevermind, I misunderstood the functionality of the code :sweat_smile: