ProfileService cannot find profile

local function playerRemoving(player)
	for _,comp in ipairs(player.leaderstats:GetChildren()) do
		if comp.Value > 0 then
			PlayerDataHandler:Set(player,comp.Name,comp.Value)
			leaderboardsHandler.Set(player,comp.Name,comp.Value)
		end
	end

	local equipped = player:GetAttribute("Equipped")
	if equipped then
		PlayerDataHandler:Set(player,"Equipped",equipped)
	end	
	
	if Profiles[player] then
		Profiles[player]:Release()
	end
end
Full module
local leaderboardsHandler = require(script.Parent.LeaderboardsHandler)

local PlayerDataHandler = {}

local dataTemplate = {
	Inventory = {"1"},
	
	Streak = 0,
	Depression = 0,
	
	Equipped = "1",
}

local ProfileService = require(script.ProfileService)
local Players = game:GetService("Players")

local ProfileStore = ProfileService.GetProfileStore(
	"PlayerProfile",
	dataTemplate
)

local Profiles = {}

--[[createFunctions for leaderstats]]
local function createValue(player,key)
	local value = Instance.new("IntValue")
	value.Value = PlayerDataHandler:Get(player,key) or 0
	value.Name = key
	value.Parent = player.leaderstats
	return value
end

local function createAttribute(player,key)
	player:SetAttribute(key,PlayerDataHandler:Get(player,key))
end

local function playerAdded(player)
	local profile = ProfileStore:LoadProfileAsync("Player_"..player.UserId)
	if profile then
		profile:AddUserId(player.UserId)
		profile:Reconcile()

		profile:ListenToRelease(function()
			Profiles[player] = nil
			player:Kick()
		end)

		if not player:IsDescendantOf(Players) then
			profile:Release()
		else
			Profiles[player] = profile
			player:SetAttribute("Ready",true)
			
			--[[EXTRA]]
			local leaderstats = Instance.new("Folder")
			leaderstats.Name = "leaderstats"
			leaderstats.Parent = player
			
			
			createValue(player,"Depression")
			createValue(player,"Streak")
			createAttribute(player,"Equipped")
			--
		end
	else
		player:Kick()
	end
end

local function playerRemoving(player)
	for _,comp in ipairs(player.leaderstats:GetChildren()) do
		if comp.Value > 0 then
			PlayerDataHandler:Set(player,comp.Name,comp.Value)
			leaderboardsHandler.Set(player,comp.Name,comp.Value)
		end
	end

	local equipped = player:GetAttribute("Equipped")
	if equipped then
		PlayerDataHandler:Set(player,"Equipped",equipped)
	end	
	
	if Profiles[player] then
		Profiles[player]:Release()
	end
end

function PlayerDataHandler:Wipe(player)
	ProfileStore:WipeProfileAsync("Player_"..player.UserId)
end

function PlayerDataHandler:Init()
	--[[
	
	
	for _,player in ipairs(Players:GetPlayers()) do
		task.spawn(playerAdded,player)
	end
	--]]
	Players.PlayerAdded:Connect(playerAdded)
	Players.PlayerRemoving:Connect(playerRemoving)
end

local function getProfile(player)
	if Profiles[player] == nil then error("No profile for" .. tostring(player)) end
	return Profiles[player]
end

function PlayerDataHandler:Get(player,key)
	local profile = getProfile(player)
	if profile.Data[key] == nil then error("No key for: " .. key) end
	return profile.Data[key]
end

function PlayerDataHandler:Set(player,key,value)
	local profile = getProfile(player)
	if profile.Data[key] == nil then error("No key for: " .. key) end
	
	assert(type(profile.Data[key]) == type(value),"Incorrect datatype")
	profile.Data[key] = value
end

function PlayerDataHandler:Update(player,key,callback)
	local profile = getProfile(player)
	local oldData = self:Get(player,key)
	local newData = callback(oldData)
	self:Set(player,key,newData)
end

return PlayerDataHandler

So I’m trying to save data before I release the profile but suddenly somewhere where the Streak IntValue in the leaderstats is iterated through in the for loop, Profiles[player] automatically becomes nil and I’m not sure why.

There is no real connection to why this is happening but I get the error:

ServerScriptService.ServerLoader.PlayerDataHandler:105: No profile forViolet_sheer

and the streak data does not save due to this random occurence of Profiles[player] randomly setting to nil.

I think I’ve realized my issue shown in the ProfileService API:

Players.PlayerRemoving:Connect(function(player)
    local profile = Profiles[player]
    if profile ~= nil then
        SaveData(profile) -- Are you sure this function doesn't error?
        profile:Release()
    end
end)

Explaining how I would be releasing the profile after the player leaves meaning Profiles[player] is nil because the player has already left.