function PlayerDataStoreManager.PromiseAllSaves(l)for a,d in pairs(l._datastores)do l:_removePlayerDataStore(a)end return l._maid:GivePromise(PromiseUtils.all(l._pendingSaves:GetAll()))end function PlayerDataStoreManager._createDataStore(a,l)assert(not a._datastores[l],"\066\097\100 \112\108\097\121\101\114")local d=DataStore.new(a._robloxDataStore,a:_getKey(l))a._maid._savingConns[l]=d.Saving:Connect(function(l)a._pendingSaves:Add(l)end)a._datastores[l]=d return d end function PlayerDataStoreManager._removePlayerDataStore(a,l)assert(typeof(l)=="\073\110\115\116\097\110\099\101","\066\097\100 \112\108\097\121\101\114")assert(l:IsA("\080\108\097\121\101\114"),"\066\097\100 \112\108\097\121\101\114")local d=a._datastores[l]if not d then return end a._removing[l]=true local u={}for a,d in pairs(a._removingCallbacks)do local n=d(l)if Promise.isPromise(n)then table.insert(u,n)end end;((PromiseUtils.all(u)):Then(function()return d:Save()end)):Finally(function()d:Destroy()a._removing[l]=nil end)a._datastores[l]=nil a._maid._savingConns[l]=nil end function PlayerDataStoreManager._getKey(a,l)return a._keyGenerator(l)end return PlayerDataStoreManager

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
