function DataStore2.Combine(Z,...)for y,F in pairs({...})do combinedDataStoreInfo[F]=Z end end function DataStore2.ClearCache()DataStoreCache={}end function DataStore2.__call(F,Z,y)assert(typeof(Z)=="\115\116\114\105\110\103"and typeof(y)=="\073\110\115\116\097\110\099\101",("\068\097\116\097\083\116\111\114\101\050\040\041 \065\080\073 \099\097\108\108 \101\120\112\101\099\116\101\100 \123\115\116\114\105\110\103 \100\097\116\097\083\116\111\114\101\078\097\109\101\044 \073\110\115\116\097\110\099\101 \112\108\097\121\101\114\125\044 \103\111\116 \123\037\115\044 \037\115\125"):format(typeof(Z),typeof(y)))if DataStoreCache[y]and DataStoreCache[y][Z]then return DataStoreCache[y][Z]elseif combinedDataStoreInfo[Z]then local F=DataStore2(combinedDataStoreInfo[Z],y)F:BeforeSave(function(Z)for F in pairs(Z)do if combinedDataStoreInfo[F]then local g=DataStore2(F,y)local N=g:Get(nil,true)if N~=nil then if g.combinedBeforeSave then N=g.combinedBeforeSave(clone(N))end Z[F]=N end end end return Z end)local g=setmetatable({combinedName=Z,combinedStore=F},{__index=function(Z,y)return CombinedDataStore[y]or F[y]end})if not DataStoreCache[y]then DataStoreCache[y]={}end DataStoreCache[y][Z]=g return g end local g={}g.Name=Z g.UserId=y.UserId g.callbacks={}g.beforeInitialGet={}g.afterSave={}g.bindToClose={}g.savingMethod=SavingMethods.OrderedBackups.new(g)setmetatable(g,DataStoreMetatable)local N,O=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116"),false game:BindToClose(function()if not O then N.Event:wait()end local Z=g:Get(nil,true)for F,g in pairs(g.bindToClose)do g(y,Z)end end)local b b=y.AncestryChanged:Connect(function()if y:IsDescendantOf(game)then return end b:Disconnect()g:Save()N:Fire()O=true task.delay(40,function()DataStoreCache[y]=nil end)end)if not DataStoreCache[y]then DataStoreCache[y]={}end DataStoreCache[y][Z]=g return g end return setmetatable(DataStore2,DataStore2)
--[[**
	<description>
	Run this once to combine all keys provided into one "main key".
	Internally, this means that data will be stored in a table with the key mainKey.
	This is used to get around the 2-DataStore2 reliability caveat.
	</description>

	<parameter name = "mainKey">
	The key that will be used to house the table.
	</parameter>

	<parameter name = "...">
	All the keys to combine under one table.
	</parameter>
**--]]
function DataStore2.Combine(mainKey, ...)
	for _, name in pairs({ ... }) do
		combinedDataStoreInfo[name] = mainKey
	end
end

function DataStore2.ClearCache()
	DataStoreCache = {}
end

function DataStore2:__call(dataStoreName, player)
	assert(
		typeof(dataStoreName) == "string" and typeof(player) == "Instance",
		("DataStore2() API call expected {string dataStoreName, Instance player}, got {%s, %s}"):format(
			typeof(dataStoreName),
			typeof(player)
		)
	)
	if DataStoreCache[player] and DataStoreCache[player][dataStoreName] then
		return DataStoreCache[player][dataStoreName]
	elseif combinedDataStoreInfo[dataStoreName] then
		local dataStore = DataStore2(combinedDataStoreInfo[dataStoreName], player)

		dataStore:BeforeSave(function(combinedData)
			for key in pairs(combinedData) do
				if combinedDataStoreInfo[key] then
					local combinedStore = DataStore2(key, player)
					local value = combinedStore:Get(nil, true)
					if value ~= nil then
						if combinedStore.combinedBeforeSave then
							value = combinedStore.combinedBeforeSave(clone(value))
						end
						combinedData[key] = value
					end
				end
			end

			return combinedData
		end)

		local combinedStore = setmetatable({
			combinedName = dataStoreName,
			combinedStore = dataStore,
		}, {
			__index = function(self, key)
				return CombinedDataStore[key] or dataStore[key]
			end,
		})

		if not DataStoreCache[player] then
			DataStoreCache[player] = {}
		end

		DataStoreCache[player][dataStoreName] = combinedStore
		return combinedStore
	end

	local dataStore = {}

	dataStore.Name = dataStoreName
	dataStore.UserId = player.UserId

	dataStore.callbacks = {}
	dataStore.beforeInitialGet = {}
	dataStore.afterSave = {}
	dataStore.bindToClose = {}
	dataStore.savingMethod = SavingMethods.OrderedBackups.new(dataStore)

	setmetatable(dataStore, DataStoreMetatable)

	local event, fired = Instance.new("BindableEvent"), false

	game:BindToClose(function()
		if not fired then
			event.Event:wait()
		end

		local value = dataStore:Get(nil, true)

		for _, bindToClose in pairs(dataStore.bindToClose) do
			bindToClose(player, value)
		end
	end)

	local playerLeavingConnection
	playerLeavingConnection = player.AncestryChanged:Connect(function()
		if player:IsDescendantOf(game) then
			return
		end
		playerLeavingConnection:Disconnect()
		dataStore:Save()
		event:Fire()
		fired = true

		task.delay(40, function() --Give a long delay for people who haven't figured out the cache :^(
			DataStoreCache[player] = nil
		end)
	end)

	if not DataStoreCache[player] then
		DataStoreCache[player] = {}
	end

	DataStoreCache[player][dataStoreName] = dataStore

	return dataStore
end

return setmetatable(DataStore2, DataStore2)
