function PlayerDataStoreManager.PromiseAllSaves(q)for O,V in pairs(q._datastores)do q:_removePlayerDataStore(O)end return q._maid:GivePromise(PromiseUtils.all(q._pendingSaves:GetAll()))end function PlayerDataStoreManager._createDataStore(O,q)assert(not O._datastores[q],"\066\097\100 \112\108\097\121\101\114")local V=DataStore.new(O._robloxDataStore,O:_getKey(q))O._maid._savingConns[q]=V.Saving:Connect(function(q)O._pendingSaves:Add(q)end)O._datastores[q]=V return V end function PlayerDataStoreManager._removePlayerDataStore(O,q)assert(typeof(q)=="\073\110\115\116\097\110\099\101","\066\097\100 \112\108\097\121\101\114")assert(q:IsA("\080\108\097\121\101\114"),"\066\097\100 \112\108\097\121\101\114")local V=O._datastores[q]if not V then return end O._removing[q]=true local d={}for O,V in pairs(O._removingCallbacks)do local u=V(q)if Promise.isPromise(u)then table.insert(d,u)end end;((PromiseUtils.all(d)):Then(function()return V:Save()end)):Finally(function()V:Destroy()O._removing[q]=nil end)O._datastores[q]=nil O._maid._savingConns[q]=nil end function PlayerDataStoreManager._getKey(O,q)return O._keyGenerator(q)end return PlayerDataStoreManager
--[=[
	Removes all player data stores, and returns a promise that
	resolves when all pending saves are saved.
	@return Promise
]=]
function PlayerDataStoreManager:PromiseAllSaves()
	for player, _ in pairs(self._datastores) do
		self:_removePlayerDataStore(player)
	end
	return self._maid:GivePromise(PromiseUtils.all(self._pendingSaves:GetAll()))
end

function PlayerDataStoreManager:_createDataStore(player)
	assert(not self._datastores[player], "Bad player")

	local datastore = DataStore.new(self._robloxDataStore, self:_getKey(player))

	self._maid._savingConns[player] = datastore.Saving:Connect(function(promise)
		self._pendingSaves:Add(promise)
	end)

	self._datastores[player] = datastore

	return datastore
end

function PlayerDataStoreManager:_removePlayerDataStore(player)
	assert(typeof(player) == "Instance", "Bad player")
	assert(player:IsA("Player"), "Bad player")

	local datastore = self._datastores[player]
	if not datastore then
		return
	end

	self._removing[player] = true

	local removingPromises = {}
	for _, func in pairs(self._removingCallbacks) do
		local result = func(player)
		if Promise.isPromise(result) then
			table.insert(removingPromises, result)
		end
	end

	PromiseUtils.all(removingPromises)
		:Then(function()
			return datastore:Save()
		end)
		:Finally(function()
			datastore:Destroy()
			self._removing[player] = nil
		end)

	-- Prevent double removal or additional issues
	self._datastores[player] = nil
	self._maid._savingConns[player] = nil
end

function PlayerDataStoreManager:_getKey(player)
	return self._keyGenerator(player)
end

return PlayerDataStoreManager
