Trying to save a Player's HumanoidRootPart's position upon leave, but character is removed too quickly

I am trying to save a Player’s HumanoidRootPart’s position with a game.Player.PlayerRemoving event, but the character is being removed before it can do the necessary processing.

game.Players.PlayerRemoving:Connect(function(player)
	local scope = "Player_"..player.UserId
	
	local RootPartPosition = player.Character:FindFirstChild("HumanoidRootPart").Position
	local XValue = RootPartPosition.X
	local YValue = RootPartPosition.Y
	local ZValue = RootPartPosition.Z

Maybe try storing the most recent position when CharacterRemoving fires and then use that value during the PlayerRemoving?

Something like:

local lastKnownPosition = {} -- map from ids to positions

local function UpdatePosition(userId, char)
    local root = char:FindFirstChild("HumanoidRootPart")
    if root then
        lastKnownPosition[userId] = root.Position
    end
end

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char) UpdatePosition(player.UserId, char) end)
    player.CharacterRemoving:Connect(function(char) UpdatePosition(player.UserId, char) end)
end)

-- when you want to save
local function SavePosition(userId)
    -- could check if the player's still in the game if you wanted
    local position = lastKnownPosition[userId]
    if position then
        -- etc
    end
end

You could also call UpdatePosition every few seconds if you wanted.

I think, I’ll just try using UpdatePosition like you said, or I’ll just have a button to save I guess.