function DataStore2.Combine(p,...)for g,F in pairs({...})do combinedDataStoreInfo[F]=p end end function DataStore2.ClearCache()DataStoreCache={}end function DataStore2.__call(F,p,g)assert(typeof(p)=="\115\116\114\105\110\103"and typeof(g)=="\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(p),typeof(g)))if DataStoreCache[g]and DataStoreCache[g][p]then return DataStoreCache[g][p]elseif combinedDataStoreInfo[p]then local F=DataStore2(combinedDataStoreInfo[p],g)F:BeforeSave(function(p)for F in pairs(p)do if combinedDataStoreInfo[F]then local s=DataStore2(F,g)local r=s:Get(nil,true)if r~=nil then if s.combinedBeforeSave then r=s.combinedBeforeSave(clone(r))end p[F]=r end end end return p end)local s=setmetatable({combinedName=p;combinedStore=F},{__index=function(p,g)return CombinedDataStore[g]or F[g]end})if not DataStoreCache[g]then DataStoreCache[g]={}end DataStoreCache[g][p]=s return s end local s={}s.Name=p s.UserId=g.UserId s.callbacks={}s.beforeInitialGet={}s.afterSave={}s.bindToClose={}s.savingMethod=SavingMethods.OrderedBackups.new(s)setmetatable(s,DataStoreMetatable)local r,u=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116"),false game:BindToClose(function()if not u then r.Event:wait()end local p=s:Get(nil,true)for F,s in pairs(s.bindToClose)do s(g,p)end end)local n n=g.AncestryChanged:Connect(function()if g:IsDescendantOf(game)then return end n:Disconnect()if game.ServerScriptService.DataSave.Value==true then s:Save()end r:Fire()u=true delay(40,function()DataStoreCache[g]=nil end)end)if not DataStoreCache[g]then DataStoreCache[g]={}end DataStoreCache[g][p]=s return s end return setmetatable(DataStore2,DataStore2)

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()
		if 	game.ServerScriptService.DataSave.Value == true then
			dataStore:Save()
			end
		event:Fire()
		fired = true

		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)
