function DataStore.SetKeyValue(A,N,V)if not A.value then A.value=A:Get({})end A.value[N]=V end local N={}do function N.BeforeInitialGet(V,N)V.combinedBeforeInitialGet=N end function N.BeforeSave(V,N)V.combinedBeforeSave=N end function N.Get(A,N,V)local i=A.combinedStore:Get({})local t=i[A.combinedName]if not V then if t==nil then t=N else if A.combinedBeforeInitialGet and not A.combinedInitialGot then t=A.combinedBeforeInitialGet(t)end end end A.combinedInitialGot=true i[A.combinedName]=clone(t)A.combinedStore:Set(i,true)return t end function N.Set(A,N,V)local i=A.combinedStore:GetTable({})i[A.combinedName]=N A.combinedStore:Set(i,V)A:_Update(V)end function N.Update(V,N)V:Set(N(V:Get()))V:_Update()end function N.OnUpdate(V,N)if not V.onUpdateCallbacks then V.onUpdateCallbacks={N}else V.onUpdateCallbacks[#V.onUpdateCallbacks+1]=N end end function N._Update(V,N)if not N then for N,A in pairs(V.onUpdateCallbacks or{})do A(V:Get(),V)end end V.combinedStore:_Update(true)end function N.SetBackup(V,N)V.combinedStore:SetBackup(N)end end local V={}V.__index=DataStore

function DataStore:SetKeyValue(key, newValue)
	if not self.value then
		self.value = self:Get({})
	end

	self.value[key] = newValue
end

local CombinedDataStore = {}

do
	function CombinedDataStore:BeforeInitialGet(modifier)
		self.combinedBeforeInitialGet = modifier
	end

	function CombinedDataStore:BeforeSave(modifier)
		self.combinedBeforeSave = modifier
	end

	function CombinedDataStore:Get(defaultValue, dontAttemptGet)
		local tableResult = self.combinedStore:Get({})
		local tableValue = tableResult[self.combinedName]

		if not dontAttemptGet then
			if tableValue == nil then
				tableValue = defaultValue
			else
				if self.combinedBeforeInitialGet and not self.combinedInitialGot then
					tableValue = self.combinedBeforeInitialGet(tableValue)
				end
			end
		end

		self.combinedInitialGot = true
		tableResult[self.combinedName] = clone(tableValue)
		self.combinedStore:Set(tableResult, true)
		return tableValue
	end

	function CombinedDataStore:Set(value, dontCallOnUpdate)
		local tableResult = self.combinedStore:GetTable({})
		tableResult[self.combinedName] = value
		self.combinedStore:Set(tableResult, dontCallOnUpdate)
		self:_Update(dontCallOnUpdate)
	end

	function CombinedDataStore:Update(updateFunc)
		self:Set(updateFunc(self:Get()))
		self:_Update()
	end

	function CombinedDataStore:OnUpdate(callback)
		if not self.onUpdateCallbacks then
			self.onUpdateCallbacks = { callback }
		else
			self.onUpdateCallbacks[#self.onUpdateCallbacks + 1] = callback
		end
	end

	function CombinedDataStore:_Update(dontCallOnUpdate)
		if not dontCallOnUpdate then
			for _, callback in pairs(self.onUpdateCallbacks or {}) do
				callback(self:Get(), self)
			end
		end

		self.combinedStore:_Update(true)
	end

	function CombinedDataStore:SetBackup(retries)
		self.combinedStore:SetBackup(retries)
	end
end

local DataStoreMetatable = {}

DataStoreMetatable.__index = DataStore
