function DataStore2.Combine(m,...)for S,P in pairs({...})do combinedDataStoreInfo[P]=m end end function DataStore2.ClearCache()DataStoreCache={}end function DataStore2.SaveAll(m)if DataStoreCache[m]then for m,S in pairs(DataStoreCache[m])do if S.combinedStore==nil then S:Save()end end end end function DataStore2.PatchGlobalSettings(m)for m,S in pairs(m)do assert(Settings[m]~=nil,"\078\111 \115\117\099\104 \107\101\121 \101\120\105\115\116\115\058 "..m)Settings[m]=S end end function DataStore2.__call(m,S,P)assert(typeof(S)=="\115\116\114\105\110\103"and typeof(P)=="\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(S),typeof(P)))if DataStoreCache[P]and DataStoreCache[P][S]then return DataStoreCache[P][S]elseif combinedDataStoreInfo[S]then local m=DataStore2(combinedDataStoreInfo[S],P)m:BeforeSave(function(m)for S in pairs(m)do if combinedDataStoreInfo[S]then local j=DataStore2(S,P)local U=j:Get(nil,true)if U~=nil then if j.combinedBeforeSave then U=j.combinedBeforeSave(clone(U))end m[S]=U end end end return m end)local j=setmetatable({combinedName=S,combinedStore=m},{__index=function(S,P)return CombinedDataStore[P]or m[P]end})if not DataStoreCache[P]then DataStoreCache[P]={}end DataStoreCache[P][S]=j return j end local j={}j.Name=S j.UserId=P.UserId j.callbacks={}j.beforeInitialGet={}j.afterSave={}j.bindToClose={}j.savingMethod=SavingMethods[Settings.SavingMethod].new(j)setmetatable(j,DataStoreMetatable)local U,l=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116"),false game:BindToClose(function()if not l then spawn(function()P.Parent=nil end)U.Event:wait()end local m=j:Get(nil,true)for S,j in pairs(j.bindToClose)do j(P,m)end end)local x x=P.AncestryChanged:Connect(function()if P:IsDescendantOf(game)then return end x:Disconnect()print(j);(((j:SaveAsync()):andThen(function()print("\112\108\097\121\101\114 \108\101\102\116\044 \115\097\118\101\100 "..S)end)):catch(function(m)warn("\101\114\114\111\114 \119\104\101\110 \112\108\097\121\101\114 \108\101\102\116\033 "..m)end)):finally(function()U:Fire()l=true end)delay(40,function()DataStoreCache[P]=nil end)end)if not DataStoreCache[P]then DataStoreCache[P]={}end DataStoreCache[P][S]=j return j end DataStore2.Constants=Constants 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.SaveAll(player)
	if DataStoreCache[player] then
		for _, dataStore in pairs(DataStoreCache[player]) do
			if dataStore.combinedStore == nil then
				dataStore:Save()
			end
		end
	end
end

function DataStore2.PatchGlobalSettings(patch)
	for key, value in pairs(patch) do
		assert(Settings[key] ~= nil, "No such key exists: " .. key)
		-- TODO: Implement type checking with this when osyris' t is in
		Settings[key] = value
	end
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(_, 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[Settings.SavingMethod].new(dataStore)

	setmetatable(dataStore, DataStoreMetatable)

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

	game:BindToClose(function()
		if not fired then
			spawn(function()
				player.Parent = nil -- Forces AncestryChanged to fire and save the data
			end)

			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()
		print(dataStore)
		dataStore
			:SaveAsync()
			:andThen(function()
				print("player left, saved " .. dataStoreName)
			end)
			:catch(function(error)
				-- TODO: Something more elegant
				warn("error when player left! " .. error)
			end)
			:finally(function()
				event:Fire()
				fired = true
			end)

		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

DataStore2.Constants = Constants

return setmetatable(DataStore2, DataStore2)
