function DataStore2.Combine(y,...)for c,T in pairs({...})do combinedDataStoreInfo[T]=y end end function DataStore2.ClearCache()DataStoreCache={}end function DataStore2.__call(T,y,c)assert(typeof(y)=="\115\116\114\105\110\103"and typeof(c)=="\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(y),typeof(c)))if DataStoreCache[c]and DataStoreCache[c][y]then return DataStoreCache[c][y]elseif combinedDataStoreInfo[y]then local T=DataStore2(combinedDataStoreInfo[y],c)T:BeforeSave(function(y)for T in pairs(y)do if combinedDataStoreInfo[T]then local F=DataStore2(T,c)local E=F:Get(nil,true)if E~=nil then if F.combinedBeforeSave then E=F.combinedBeforeSave(clone(E))end y[T]=E end end end return y end)local F=setmetatable({combinedName=y,combinedStore=T},{__index=function(y,c)return CombinedDataStore[c]or T[c]end})if not DataStoreCache[c]then DataStoreCache[c]={}end DataStoreCache[c][y]=F return F end local F={}local E=y..("\047"..c.UserId)F.dataStore=DataStoreService:GetDataStore(E)F.orderedDataStore=DataStoreService:GetOrderedDataStore(E)F.name=y F.player=c F.callbacks={}F.beforeInitialGet={}F.afterSave={}F.bindToClose={}setmetatable(F,DataStoreMetatable)local H,l=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116"),false game:BindToClose(function()if not l then H.Event:wait()end local y=F:Get(nil,true)for T,F in pairs(F.bindToClose)do F(c,y)end end)Players.PlayerRemoving:connect(function(y)if y==c then F:Save()H:Fire()l=true delay(40,function()DataStoreCache[y]=nil end)end end)if not DataStoreCache[c]then DataStoreCache[c]={}end DataStoreCache[c][y]=F return F 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 = {}
	local dataStoreKey = dataStoreName .. "/" .. player.UserId

	dataStore.dataStore = DataStoreService:GetDataStore(dataStoreKey)
	dataStore.orderedDataStore = DataStoreService:GetOrderedDataStore(dataStoreKey)
	dataStore.name = dataStoreName
	dataStore.player = player
	dataStore.callbacks = {}
	dataStore.beforeInitialGet = {}
	dataStore.afterSave = {}
	dataStore.bindToClose = {}

	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)

	Players.PlayerRemoving:connect(function(playerLeaving)
		if playerLeaving == player then
			dataStore:Save()
			event:Fire()
			fired = true

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

	--[[spawn(function()
		player:WaitForChild("ClientSaveRequest")
		function player.ClientSaveRequest.OnServerInvoke()
			dataStore:Save()
			return
		end
	end)]]

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

	DataStoreCache[player][dataStoreName] = dataStore

	return dataStore
end

return setmetatable(DataStore2, DataStore2)
