local c=game:GetService("\068\097\116\097\083\116\111\114\101\083\101\114\118\105\099\101")local T={}T.__index=T function T.Get(c)local T,m=pcall(function()return((c.orderedDataStore:GetSortedAsync(false,1)):GetCurrentPage())[1]end)if not T then return false,m end if m then local T=m local B=T.value c.dataStore2:Debug("\109\111\115\116 \114\101\099\101\110\116 \107\101\121",T)c.mostRecentKey=B local u,G=pcall(function()return c.dataStore:GetAsync(B)end)if not u then return false,G end return true,G else c.dataStore2:Debug("\110\111 \114\101\099\101\110\116 \107\101\121")return true,nil end end function T.Set(T,c)local m=(T.mostRecentKey or 0)+1 local B,u=pcall(function()T.dataStore:SetAsync(m,c)end)if not B then return false,u end local G,Y=pcall(function()T.orderedDataStore:SetAsync(m,m)end)if not G then return false,Y end T.mostRecentKey=m return true end function T.new(m)local B=m.Name..("\047"..m.UserId)local u={dataStore2=m,dataStore=c:GetDataStore(B);orderedDataStore=c:GetOrderedDataStore(B)}return setmetatable(u,T)end return T


local DataStoreService = game:GetService("DataStoreService")

local OrderedBackups = {}
OrderedBackups.__index = OrderedBackups

function OrderedBackups:Get()
	local success, value = pcall(function()
		return self.orderedDataStore:GetSortedAsync(false, 1):GetCurrentPage()[1]
	end)

	if not success then
		return false, value
	end

	if value then
		local mostRecentKeyPage = value

		local recentKey = mostRecentKeyPage.value
		self.dataStore2:Debug("most recent key", mostRecentKeyPage)
		self.mostRecentKey = recentKey

		local success, value = pcall(function()
			return self.dataStore:GetAsync(recentKey)
		end)

		if not success then
			return false, value
		end

		return true, value
	else
		self.dataStore2:Debug("no recent key")
		return true, nil
	end
end

function OrderedBackups:Set(value)
	local key = (self.mostRecentKey or 0) + 1

	local success, problem = pcall(function()
		self.dataStore:SetAsync(key, value)
	end)

	if not success then
		return false, problem
	end

	local success, problem = pcall(function()
		self.orderedDataStore:SetAsync(key, key)
	end)

	if not success then
		return false, problem
	end

	self.mostRecentKey = key
	return true
end

function OrderedBackups.new(dataStore2)
	local dataStoreKey = dataStore2.Name .. "/" .. dataStore2.UserId

	local info = {
		dataStore2 = dataStore2,
		dataStore = DataStoreService:GetDataStore(dataStoreKey),
		orderedDataStore = DataStoreService:GetOrderedDataStore(dataStoreKey),
	}

	return setmetatable(info, OrderedBackups)
end

return OrderedBackups
