How to teleport character when jumping out of a seat?

So I’ve made a semi-custom seat implementation for my game: the seat is disabled, to prevent accidental sitting, and instead relies on the player looking at the seat and pressing E (or LMB) to sit. The seat then stores the character HumanoidRootPart’s CFrame, and uses the Sit method to… well… sit the character.

When the player jumps out of the seat, I want to teleport the character to where they were before they were sat using the stored CFrame, but I can’t. I set the CFrame, but the character still jumps out of the seat like normal and stays on top of the seat until moved by the player. It’s pretty jarring, too, as you’re going from looking at a terminal screen to suddenly being in first-person and jumping. Here’s my code:

local char = nil
local char_cf = nil

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    if seat.Occupant then
	char = seat.Occupant.Parent
	char_cf = char.HumanoidRootPart.CFrame
    elseif char_cf then
	char.HumanoidRootPart.CFrame = char_cf
	char = nil
	char_cf = nil
    end
end)

I’ve tried adding a wait() before setting the HumanoidRootPart’s CFrame, and also tried waiting for the SeatWeld to stop existing (just to be sure), but the character still seems to ignore being teleported. What am I doing wrong?

1 Like

char and char_cf are defined outside that function (I’ll just add that in, oops), so they do persist.

1 Like

…yeah I read your code wrong.

Have you tried just doing this?

repeat 
  wait()
  char.HumanoidRootPart.CFrame = char_cf
until char.HumanoidRootPart.CFrame == char_cf 

Roblox is REALLY buggy with stuff like this, I had a game which needed to teleport people in and out of seats a lot and it was pretty much guaranteed to fail to teleport the player at some point no matter what.

1 Like

Still no dice, I’m afraid. The character still acts as if it’s jumping out of a seat normally.

At this point, I’m considering a fully custom seat system.

I have a feeling that once Occupant is set then it’s too late to grab the CFrame. It might already set the CFrame to be where the seat is before firing that event. Or at least introduce a race-condition possibly?

Instead, I would try to grab the CFrame value from whatever code is also handling the clicks or whatever. Something before Occupant is set.

2 Likes

Ah, that’s a fair point. I even thought about that earlier, but for some reason decided at the time it wasn’t the problem.

Edit: Yeah, that was the problem. Kudos!

1 Like