[SOLVED] Teleport from VehicleSeat to an Attachment

  1. What do you want to achieve? I’m currently making a vehicle, but the thing is that the vehicle’s body is closed and has collisions, so I want that when a player leaves the DriverSeat / VehicleSeat, he’s teleported to an Attachment location

  2. What is the issue? I don’t find a way to get the player character after leaving the VehicleSeat

  3. What solutions have you tried so far? Found a similar post, different thing in some ways

local prompt = script.Parent
local seat = prompt.Parent

prompt.Triggered:Connect(function(plr)
	local char = plr.Character
	local hum = char:FindFirstChildOfClass("Humanoid")
	
	if not seat.Occupant then
		seat:Sit(hum)
	else
		return
	end
end)

seat.Changed:Connect(function(property)
	if property == "Occupant" then
		if seat.Occupant then
			prompt.Enabled = false
		else
			local plr = game.Players:GetPlayerFromCharacter(seat.Occupant)
			local char = plr.Character
			local exitpos = prompt.Parent:WaitForChild("ExitPos").Position
			
			local hrp = char:FindFirstChild("HumanoidRootPart")
			local torso = char:FindFirstChild("Torso")
			
			if hrp then
				hrp.Position = exitpos
			elseif torso then
				torso.Position = exitpos
			end
			
			prompt.Enabled = true
		end
	end
end)

The code is from the prompt I use to enter the VehicleSeat, btw

1 Like

you can just weld a part to the side of the door which you want the player to exit from instead of using attachment. then when they leave the seat > teleport them to said part

Script if you don't know an easy way to do that
local Players = game:GetService("Players")
local seat = script.Parent
local LeavePart = script.Parent.Parent.LeavingPart

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant then -- when someone sit on the seat, remembering their userid
		local player = Players:GetPlayerFromCharacter(seat.Occupant.Parent)
		seat:SetAttribute("Current_Occupant", player.UserId)
	else -- when they leave the seat, teleport the player with the same userid to the leaving part outside and delete the current Id
		local leavingOccupant = Players:GetPlayerByUserId(seat:GetAttribute("Current_Occupant"))
		leavingOccupant.Character.PrimaryPart.CFrame = LeavePart.CFrame
		seat:SetAttribute("Current_Occupant", nil)
	end
end)

script in action

if you have a question feel free to ask!

You know a way to avoid the player getting thru walls or things like that? Because I think that by that way, the character can get stuck and bugs like that when leaving the vehicle and getting teleported.

Thanks btw, I will try that script soon :wink:

I don’t know what happens, but the car starts to behave a little bit weird…

(I tested a little bit before recording the video and it can also make the car go under the map)

You can raycast from the side of the hrp to get the closest position and then teleport him there.

local rp = RaycastParams.new()
rp.FilterDescendantInstances = {character, car}
rp.FilterType = Enum.RaycastFilterType.Exclude

local lastOccupant = nil
local teleportDistance = 10

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    if not seat.Occupant and lastOccupant then
      local ray =  workspace:Raycast(lastOccupant.Parent.PrimaryPart.Position, seat.CFrame.RightVector * teleportDistance, rp)
      local pos = seat.Position + seat.CFrame.RightVector * (ray and ray.Distance or teleportDistance)
      lastOccupant.Parent:SetPrimaryPartCFrame(CFrame.new(pos))
      lastOccupant = nil
    elseif seat.Occupant then
      lastOccupant = seat.Occupant
    end
end)
1 Like

I have a problem by using this script, I want the teleport being in the left, but this script makes my character be teleported to the right of the vehicle, and well, “CFrame.LeftVector” doesn’t exist so what I can do? ← I figured this out already btw

And also, this code makes my character go thru walls, so what I could do to teleport the player to the nearest “safe location”? (I mean, where is not gonna go thru walls or end up falling to the void)

LeftVector is just Negative RightVector

do CFrame.RightVector * -1 before you do the raycast

1 Like

Thanks for the help again! :wink:

Also, I wanted to ask, do you know how to avoid the player getting thru walls if it’s too close to a wall when leaving the vehicle?

Thats what the script is doing. It checks if theres an object, if there is it teleports to behind the object else it teleports to the normal distance. For changing it to the left, just multiply it by -1. You might have to assign each seat a value to refer to so that you can have the right side seats tp to the right and the other to the left.

You can offset the ray distance.
Edit the “pos” variable to this

local distanceOffset = 2 -- how much behind the object it tps
local pos = seat.Position + seat.CFrame.RightVector * (ray and math.max(ray.Distance - distanceOffset, 0) or teleportDistance)

1 Like

When sitting you’re welded to the seat … You’ll have to stop sitting then move the player.

A quick test in the studio

local seat = workspace.Vehicle.VehicleSeat
local attachment = seat:WaitForChild("Attachment")

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local occupant = seat.Occupant
	if occupant then 
		
		task.wait(3)-- pause for this test
		
		occupant.Sit = false task.wait(0.1) -- needed
		local character = occupant.Parent
		local rootPart = character:FindFirstChild("HumanoidRootPart")
		if rootPart then
			rootPart.CFrame = attachment.WorldCFrame
		end
	end
end)

In this my attachment is in the seat itself. The attachment is moved over to outside the car door,
as it is only a placement refence at this point. Collisions shouldn’t matter, I use something like this myself.

This is probably all you need to add to your script…

occupant.Sit = false task.wait(0.1)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.