Data handling methods

So currently, I’m working on a data handling system that uses pcalls as a safety measure.
Loading Data

  • The player loads in, checks if they have data, loads in data

Saving Data

  • When the player leaves, saves data
  • When the server shuts down, saves data via BindToClose()
  • Auto Saves every 2 minutes

Forgot to mention, the save function below is called when the list above fires it. - Edit

Are these the best methods to save? (I made it similar to the robox wiki version)

local tries = 0	
	local success
	repeat
		tries = tries + 1
		success = pcall(function()
			print("saved")
			Data:SetAsync(prefix  .. tostring(player.UserId), {
				-- Data 
			})
		end)
		if not success then wait(1) end
	until tries == 3 or success
	if not success then
		warn("Cannot save data for player!")
	end
game:BindToClose(function()
    for _, client in ipairs(game:GetService("Players"):GetPlayers()) do
        spawn(function()
            saveData(client)
        end)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	saveData(player)
	
end)

Yes it is safe to use these methods however I would suggest looking at other data store tutorials for better ideas

1 Like

I’ve tried to something similar to what you have done here, and ran into a problem. The loop for trying to save a data file should be spaced out longer - I know that you are only asking for 3 tries, but Roblox considers it spam if you do it in frequent time, so it will throw an warning sometimes (try it!)

Consider 5 to 7 seconds as a safer alternative. I’m pretty sure that 7 seconds is the average time you can call setAsync anyways… (I may be wrong, this is worth checking for yourself).

Hope this helps

1 Like

The automatic retries feature on datastores is disabled I would recommend creating a nested retry function.

For example;

local function Retry(func)
    local tries = 0
    local success = false
    local data = nil
    repeat
        success = pcall(function() data = func() end)
        if not success then
            tries = tries + 1
            wait(1)
        end
    until success or tries >= 3
    return data
end

Or be smart and use DataStore2

1 Like