Best approach if datastore requests fail?

If I am saving a player’s data and the request fails.
Should I try wait a random amount of time and try again?

What is the best approach for this?

2 Likes

You can wrap the request in a pcall so that if the request fails it doesn’t end the script there.

Helpful post: Pcalls - When and how to use them - Resources / Community Tutorials - DevForum | Roblox

2 Likes

@ComplicatedParadigm is right. If you wrap your request in a pcall(), you are given the chance to retry after some time, instead of the script breaking.

1 Like

Yeah I’m aware. But if it fails… what then? Do I just try again or should I be trying to save the player data off somewhere else?

I suppose I can’t answer that directly, since I haven’t run into problems in a working game yet with the datastore. But I think it can depend on what caused the problem. Probably you can set it up to retry after some amount of time.

use a repeat loop to keep trying to save the data, with maybe 10-20 second wait in between retries. And stop when the pcall return success

Yeah I was thinking that but it feels a bit crude, and could lead to issues with too many requests.

There is no need since 10-20 seconds is plenty, but if you are unsure you can always use :GetRequestBudgetForRequestType() and wait a longer time if it gets too low

If it keeps on failing, is there a point where you should give up and let that player’s session data be lost?

It is unlikely that it keeps failing, since that will probably mean that the servers are having problems instead of the client having lag spikes. But in the case that it does happen, you can keep trying since there is not really much harm, but you might want to have a gui that warns the players that data is not being saved correctly, and tell them to take screenshots just in case data loss occurs.

What I have done with my games, is this.

Data Failing to Load: First, I retry 3 times. I space each retry out by 5 seconds or so, just to be safe.
Notify the player their data has failed to load, and that they may rejoin to try again. Let them know that since their data has failed to load, any progress made in this session will not save (to prevent overwriting their old dataset). Also allow them to report it to you somehow as a bug.

Data Failing to Save: Retry 3 times, with each spaced out by 5 seconds. If this doesnt work, then push the players save to an external location (I use a discord webhook and encode their data into a JSON string, and in the webhook I @ the developers of my server). This way, the developers can reach out to the member, and let them know that their data had problems and they are working to recover it. To actually recover it, you can just JSONDecode the data table pushed through the webhook, and upload it to the datastore manually to confirm it works. Then, obviously, figure out why the system failed to save in the first place.

5 Likes

The warning GUI is a good idea. What should you do if this happens while they are leaving the game though?

Really this is a tought decision beacuse at that point either A you try your best and use a loop to get there player data back or they are forced to have lost there data. If this is happening try to clean stuff up and create tables for data. e.g below:

db:SetAsync(player.UserId.." Data",{["field1"] = "foo",["field2"] = "boo"})

--and act as a table instead. e.g

local data = db:GetAsync(player.UserId.." Data")
print(data["field1"])

You never have to lose the data unless roblox is having server problems, because you can always push the data to a backup location through http service. For example, a discord webhook.

Also: When roblox is having server problems, make a GUI in your game that can pop up and notify players data wont save. You can detect such problems by monitoring the overall results of get and set async commands to datastores.

That won’t really work since the minimum cooldown is 6 seconds

That is not true, there is no time restriction for FAILED calls, just a quantitative restriction. I believe the maximum amount a datastore can be called is:

2 Likes

Well true but it’d be a pain if its a big game and he has to add all the data back. Plus there are also rate limits for that.

Thats why I would also make a UI that shows up when roblox is having server problems; See my above comment.

1 Like