Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

Handling worst-case scenarios with DataStoreService?

Asked by
Optikk 499 Donator Moderation Voter
4 years ago

Suppose a player's data is saved upon leaving. After retrying many times (5 times or more) to save their data via DataStoreService, the data still fails to save. What should I do? Cross my fingers that this never happens? If anyone else has taken into consideration this problem, have you taken any precautions for a situation like this? I'm hoping to get several potential workable solutions since to me it seems that there's no cure-all for this problem.

Here's some fake code modeled after the real code I'm using if needed:

function SaveData(params)
    local success, value = pcall(function()
        DataStore:SetAsync(params.key, params.value)
    end)

    if not success then
        warn('could not save data')

        -- still some attempts left
        if params.attempts > 0 then
            wait(params.retryInterval)
            params.attempts -= 1
            return SaveData(params)

        -- data could not be saved after exhausting all attempts
        else
            return false
        end
    else
        -- data was saved, return true
        return true
    end
end

Players.PlayerRemoving:Connect(function(player)
    local success = SaveData({
        attempts = 5,
        retryInterval = 3,
        key = player.UserId,
        value = player.Points.Value
    })

    if not success then
        -- what now?
    end
end)

1 answer

Log in to vote
1
Answered by 4 years ago

This is a great question, and somthing that's important to consider if you want to have a really polished game.

The only reasonable solution I can see to solve this is to cache all failed saves in the datastore script to attempt re-saving after a much longer time interval (~10-30min) until it works. Hopefully, this would allow the problem resolve itself on Roblox's side.

However, what if the server is forced to shut down? This is a much trickier senario, IMO, and at this point I think you'd be justified in letting progress disappear (after one last save attempt). If you were really dedicated, you might be able to transfer their data to another server using Roblox's MessagingService and let that one continue the save attempts, but it would be far more effort than it would be worth.

A Better Solution

Ultimately, I'd argue, the problem isn't so much the loss of the data - it's the expectation of the player that it wouldn't be lost. In the classic Roblox building games of old - back when seamless saving wasn't a thing - nobody was upset when their creations were lost once they logged off because that loss was expected. The strife comes when a player thinks their creation WILL be saved and comes back to find it gone.

To play to this, I'd suggest implementing semi-frequent autosaves and notifying the player when an issue occurs! If you tamp a player's expectation of their data being saved with a message like

An error occurred saving your data. If you log off your progress may not be saved.

then the nasty surprise of logging in the next day to find nothing becomes annoyance rather than rage.

If auto-saving isn't doable, then I'd still make sure to track and notify other players if a save failure happens to more than one leaving player. Multiple failures could indicate a global problem with DataStore, and lowering expectations is probably all that you can do at that moment. In this notification you could also try and keep players online and in the server to keep it alive (and saving) until the DataStore service is fixed.

1
This is one of the best answers I've seen on this website so far. You bring up a lot of great points. Thank you! Optikk 499 — 4y
Ad

Answer this question