The Character isn't found in PlayerRemoving

The print(character) doesnt work. Looks like the if statement doesnt go through, it should print the character.
https://gyazo.com/2c6ffb64fcd2731f44a6f17a6c428736

game:GetService("Players").PlayerRemoving:Connect(function(plr)
    local character = plr.Character
    if character then
        SpawnBackpack(plr:FindFirstChild("Inventory"), plr, character)
        print(character) -- It should print this, but it doesnt like seen in the video
    end
    local profile = ProfileManagerModule.Profiles[plr]
    if not profile then return end
    profile:Release()
end)

The only print() that works is inside of ListenToRelease()

I have also tried CharacterRemoving

game:GetService("Players").PlayerRemoving:Connect(function(plr)
	local profile = ProfileManagerModule.Profiles[plr]
	if not profile then return end
	profile:Release()
	
	plr.CharacterRemoving:Connect(function(character)
		if character then
			SpawnBackpack(plr:FindFirstChild("Inventory"), plr, character)
			print(character)
		end
	end)

end)

but still doesnt work

Connect CharacterRemoving in PlayerAdded.
Then check if the player is leaving somehow
(This is just meant to give you an idea of what needs to be done, it’s not the full code!)

okay, so I have to put the CharacterRemoving event inside of PlayerAdded event? Is there a function or something to check if the player is leaving (other than PlayerRemoving)?

You could try task.wait-ing a bit and seeing if the player still exists, then doing what ever you need to do.
(I’m pretty sure player character’s are only parented to nil, not entirely deleted, meaning the character still exists, it’s just not visible to you)

you say that I should do task.wait() inside of PlayerRemoving?

No, inside the character removing, add a task.wait(0.5), check if player exists, and then if the player parent is not nil.

when the player is removing his character is destroyed

The print() inside of ListenToRelease() works
plr.CharacterRemoving:Connect(function(character)
if character then
SpawnBackpack(plr:FindFirstChild(“Inventory”), plr, character)
print(character) – This is the only print() that works
end
end)

I also tried CharacterAutoLoads, but it doesn’t work
game:GetService(“Players”).PlayerRemoving:Connect(function(plr)
local profile = ProfileManagerModule.Profiles[plr]
if not profile then return end
profile:Release()
plr.CharacterAutoLoads:Connect(function(character)
if character then
print(character)
SpawnBackpack(plr:FindFirstChild(“Inventory”), plr, character)
end
end)
end)

Thanks in advance!
Edit: this is the whole code.
game:GetService(“Players”).PlayerRemoving:Connect(function(plr)
local profile = ProfileManagerModule.Profiles[plr]
if not profile then return end
profile:Release()
plr.CharacterRemoving:Connect(function(character)
if character then
print(character)
SpawnBackpack(plr:FindFirstChild(“Inventory”), plr, character)
end
end)
end)

–[[
Profiles when created will load in a player’s inventory and equipment slots.
This means that if a player’s inventory is empty but he had a backpack equipped,
after rejoining he would be equipped with the backpack.
This function removes a player’s equipment and creates a backpack where the player was standing.
This backpack contains all the equipment that was previously equipped.
]]
function SpawnBackpack(inventory, plr, character)
for _, slot in next, InventorySlots do
local item = plr[slot.Instance.Name]
if item then
inventory:AddItem(item)
plr[slot.Instance.Name] = nil
end
end
local backpack =