Detecting characters last position when he/she leaves

I have a weird problem, when the character leaves I want to drop the flag in the last position he was before he left. However when PlayerRemoving is fired the character object is already destroyed, how would I get the last position?

Yah I could constantly save the characters position in a table, but that seems inefficient.

I’m by no means a great scripter, but could you use this to detect when the Player is dead and drop to drop the flag before they die?

Yah I already drop it when they die. When the player is leaving I don’t think he’s killed, the character is removed.

Or are you suggesting to drop the flag any time a character is removed from the game? That might be a good idea since that fires when either the player dies or the player is leaving from the game and his character his/she is removing.

game.Players.PlayerRemoving:Connect(function(player)
  local lastposition = player.Character.HumanoidRootPart.Position
end)

edit: it shouldnt be destroyed, do you have any scripts interfering with it?

That doesn’t work

I already tried that.

My bad, have you tried storing player positions in a dictionary that updates once every .2 seconds or so? And just accessing that when they leave?

Yah that might be a good idea, let me try both solutions from you and the other guy.

Initially I have thought of the dictionary idea, but I was looking for more an event based solution for efficiency but yah I’ll give both a go.

1 Like

Acually,
Just use
Player.CharacterRemoving.

local function CharacterRemoving()
		local CFrameComponents = Root.CFrame:components()  -- Root is humanoid root part
		CFrameComponents = HttpService:JSONEncode({Root.CFrame:components()}) -- Ready to save!
	end

Then just save it!
(to retrieve use JSONDecode()
Connect function to Player.CharacterRemoving, obviously!

You could also possibly try using CharacterRemoving, as it might fire before the Character is removed. You would of course have to check if the Player is nil afterwards to make sure that the user left the game, and didn’t just reset.

1 Like

Yah that might be a good solution to, i’ll try all of them.

Posted a solution to the same problem here:

1 Like

The problem it’s a bit funky sometimes PlayerRemoving still has the character reference and sometimes it doesnt.

That’s why using CharacterRemoving as well should give more consistent results, as I mentioned. Take a look at @nicemike40’s code. I think that is the optimal type of method you should use.

Yah I’m going to give that a try to, it seems like the best method.