How can I make teleportation more effcient and quick? (Car)

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Hi! I’m @mcox6 and I am wondering how I can make teleportation more quick and efficient (as stated in the title). For this to make any sense, you may have to watch the video below

https://gyazo.com/707cb4f7a595a31af88d05bacd019857

Now, that example isn’t the best, but you can see a slight delay before the player is actually ejected, here is my script so you can see how I’m doing this.

print(script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.DriveSeat.Occupant)
local seat = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.DriveSeat
script.Parent.Triggered:Connect(function(player)
	player.Character.HumanoidRootPart.CFrame = seat.CFrame
	wait(1)
	print(seat.Occupant)
	
	if seat.Occupant ~= nil then

		script.Parent.Enabled = false	
		print("Im use less :(")
	end
	
	while true do
		if seat.Occupant == nil then
			
			if script.Parent.Enabled == false then
				player.Character.HumanoidRootPart.CFrame = seat.CFrame + Vector3.new(10,0,0)
				wait(.1)
				print("n")
				script.Parent.Enabled = true
			else
				
			end
		end
		wait(.5)
	end
		
		
	
end)

When the player presses space the seat.Occupant property becomes nil. When this happens I eject the player and move them 10 studs away from the seat they were in. The only problem is, when moving at high speeds or even stationary the player is in the car (but not seated) for a short period of time.

My objective is to have the player immediately teleported outside of the car rather than a split second, then a teleport.

If anyone has solutions it would be highly appreciated! :smiley:

Here’s a picture of me in the car before being teleported out

  1. You can use Seat:Sit() to force a player to sit in a seat instead of setting the CFrame like you’re doing.

  2. You can use seat:GetPropertyChangedSignal("Occupant"):Connect to use an event instead of a loop, and execute the code as soon as the occupant changes.

Put together with some code from docs:

(untested)

local seat = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.DriveSeat

script.Parent.Triggered:Connect(function(player)
	if not seat.Occupant then
		local character = player.Character
		if character then
			local humanoid = char:FindFirstChild("Humanoid")
			if humanoid then
				seat:Sit(humanoid)
			end
		end
	end
end)

local currentPlayer = nil

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local humanoid = seat.Occupant 

	if humanoid then
		-- someone sat down (possibly triggered by above code)
		script.Parent.Enabled = false

		local character = humanoid.Parent
		local player = Players:GetPlayerFromCharacter(character)

		if player then 
			print(player.Name .. " has sat down")
			currentPlayer = player
		end	
	elseif currentPlayer then
		-- someone got up
		script.Parent.Enabled = true

		print(currentPlayer.Name .. " has got up")

		local character = currentPlayer.Character

		if character then
			local root = character:FindFirstChild("HumanoidRootPart")
			if root then
				root.CFrame = seat.CFrame * CFrame.new(10, 0, 0)
			end
		end

		currentPlayer = nil
	end
end)

Note how the logic for what happens when someone sits down or gets up was moved into event handler instead of the trigger. So whether someone sits in the seat because of the trigger or because they touched the seat, the effect is the same.

1 Like

Hey! I’ve been experimenting with this code for a while and I was wondering if you could answer a question. Do you know why when I get up from the seat I am stationary? I’ve done some prints and it shows that i stay where I am regardless the CFrame movement. Thanks!