I want to teleport a seated player to a specific position/CFrame.
the things I tried:
1:
char:MoveTo(game.Workspace.Part.Position)
--this only makes the character stand up.
2:
char.HumanoidRootPart.CFrame = game.Workspace.Part.CFrame
--this makes the character teleport to the specific CFrame TOGETHER with the seat.
3:
char.Humanoid.Sit = false
wait()
char:MoveTo(game.Workspace.Part.Position)
--this is a good solution - the only problem is that MoveTo() is used:
--the problem with this is that if there are any obstacles (collidable or non-collidable), the character gets teleported on top of them
4:
char.Humanoid.Sit = false
wait(0.2) --seems reliable? turning this to just wait() is not reliable.
--repeat wait() until char.Humanoid.Sit == false is also not reliable.
char.HumanoidRootPart.CFrame = game.Workspace.Part.CFrame
My question:
What is the best and most reliable way to teleport a character out of a seat?
The first method is not a solution. It only shows that when using MoveTo() without unseating the character first, the character only stands up without teleporting to the position/CFrame.
I don’t really know if there is a best way to do it, but to prevent from the player sitting again, I would teleport them a couple studs infront of them.
char.Humanoid.Sit = false
wait(0.2)
char.HumanoidRootPart.CFrame = char.HumanoidRootPart.CFrame * CFrame.new(Vector3.new(0, 0, -2)) -- offset, you gotta play around with this
-- moveto()
The problem with this is that the wait(0.2) is not 100% reliable. At least not for me. There could be a situation where the wait(0.2) is not long enough for the weld to get destroyed properly resulting in the player teleporting to the CFrame together with the seat
I’ve tried spamming this over 100 times so far in studio, and it works fine 100% of the time. it shouldn’t cause any issue cause the check is made from the live server.
Using the code below works, but I get the same result as method 4. Not knowing how long the wait() has to be. Since wait() is too fast. But wait(0.2) not. So there may be cases where wait(0.2) could not be fast enough
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
local Humanoid = script.Parent.Occupant
if not Humanoid then
return
end
connection = Humanoid.StateChanged:Connect(function(prevState, newState)
if prevState == Enum.HumanoidStateType.Seated then
Humanoid.Parent.HumanoidRootPart.CFrame = CFrame.new(0, 10, 0)
connection:Disconnect()
end
end)
end)