--All loading and saving data should be done in this module
--Currently uses DataPersistence but could be changed to use a DataStore easily

local dataHandler = {}

local function recurLoadNumber(key, player, depth)
	local ok, number = pcall(function() return player:LoadNumber(key) end)
	if not ok or number == 0 then
		if depth >= 5 then
			return number
		else
			return recurLoadNumber(key, player, depth + 1)
		end
	end
	return number
end

local function recurSaveNumber(key, player, depth, value)
	local ok = pcall(function() player:SaveNumber(key, value) end)
	if not ok then
		if depth < 5 then
			return recurSaveNumber(key, player, depth + 1, value)
		end
	end
end

function dataHandler:LoadNumber(player, key)
	return recurLoadNumber(key, player, 0)
end

function dataHandler:SaveNumber(player, key, value)
	return recurSaveNumber(key, player, 0, value)
end

return dataHandler
