local y=game:GetService("\068\097\116\097\083\116\111\114\101\083\101\114\118\105\099\101")local g={}g.__index=g function g.Get(y)local g,H=pcall(function()return((y.orderedDataStore:GetSortedAsync(false,1)):GetCurrentPage())[1]end)if not g then return false,H end if H then local g=H local k=g.value y.dataStore2:Debug("\109\111\115\116 \114\101\099\101\110\116 \107\101\121",g)y.mostRecentKey=k local o,T=pcall(function()return y.dataStore:GetAsync(k)end)if not o then return false,T end return true,T else y.dataStore2:Debug("\110\111 \114\101\099\101\110\116 \107\101\121")return true,nil end end function g.Set(g,y)local H=(g.mostRecentKey or 0)+1 local k,o=pcall(function()g.dataStore:SetAsync(H,y)end)if not k then return false,o end local T,w=pcall(function()g.orderedDataStore:SetAsync(H,H)end)if not T then return false,w end g.mostRecentKey=H return true end function g.new(H)local k=H.Name..("\047"..H.UserId)local o={dataStore2=H,dataStore=y:GetDataStore(k),orderedDataStore=y:GetOrderedDataStore(k)}return setmetatable(o,g)end return g
--[[
	berezaa's method of saving data (from the dev forum):

	What I do and this might seem a little over-the-top but it's fine as long as you're not using datastores excessively elsewhere is have a datastore and an ordereddatastore for each player. When you perform a save, add a key (can be anything) with the value of os.time() to the ordereddatastore and save a key with the os.time() and the value of the player's data to the regular datastore. Then, when loading data, get the highest number from the ordered data store (most recent save) and load the data with that as a key.

	Ever since I implemented this, pretty much no one has ever lost data. There's no caches to worry about either because you're never overriding any keys. Plus, it has the added benefit of allowing you to restore lost data, since every save doubles as a backup which can be easily found with the ordereddatastore

	edit: while there's no official comment on this, many developers including myself have noticed really bad cache times and issues with using the same datastore keys to save data across multiple places in the same game. With this method, data is almost always instantly accessible immediately after a player teleports, making it useful for multi-place games.
--]]

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
