How do I make a part that teleports a player to a different area depending on the part they touched

Hi, I’m trying to create a script that teleports a player to a different part of the game, but when they teleport back they will teleport to the area they teleported from. I know this doesn’t make much sense so allow me to explain!
Say I have two parts in the workspace named Trainpart1 and Trainpart3. When a player touches those parts, it teleports the player to the same place; lets call this place area2 (referance/not in the workspace). By area2 there is another part in the workspace named Trainpart2 which will teleport the player back to area1 (where Trainpart1 and 3 are at). However, If a player teleports to area2 using Trainpart3, I want Trainpart2 to teleport the player close to Trainpart3. If the player teleports to area2 using Trainpart1, I want Trainpart2 to teleport the player close to Trainpart1. I tried using a NumberValue by cloning it once a player touches Trainpart3 and when the player Touches Trainpart2 it will check if the player has that value and if they dont it will teleport them near Trainpart1, if they do have the value it will teleport them near Trainpart3 and destroy the value. This didn’t work and it was much too complicated and cluttered and I’m not sure if theres a more efficient way to script this.
More:
Screen Shot 2023-09-19 at 9.48.56 PM

--TeleportTrain1
script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent:FindFirstChild("Humanoid") then
		if canhit == true then
		end
		canhit = false
		local human = hit.Parent:FindFirstChild("HumanoidRootPart")
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		wait(1)
		human.CFrame = CFrame.new(Vector3.new(-779.621, 0.353, 173.448)) --Area near TrainPart2
		canhit = true
	end
end)
--TeleportTrain3
local canhit = true

script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent:FindFirstChild("Humanoid") then
		if canhit == true then
		end
		canhit = false
		local human = hit.Parent:FindFirstChild("HumanoidRootPart")
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		wait(1)
		human.CFrame = CFrame.new(Vector3.new(-779.621, 0.353, 173.448)) --Area near Trainpart2
		canhit = true
	end
end)
TeleportTrain2
local canhit = true

script.Parent.Touched:Connect(function(hit)
		if hit and hit.Parent:FindFirstChild("Humanoid") then
		if canhit == true then
		end
		canhit = false
		local human = hit.Parent:FindFirstChild("HumanoidRootPart")
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		wait(1)
		human.CFrame = CFrame.new(Vector3.new(-583.262, -1.548, 57.101)) --Area near Trainpart1 (I do not have code about the other part)
		canhit = true
	end
end)

Sorry for any confusion!

1 Like

Why are you repositioning the humanoid shouldn’t you be repositioning the HumanoidRootPart?

"Your objective is to teleport a player to a specific Destination when they interact with a corresponding Teleporter, correct?

If you don’t require player access checks, the most efficient approach is to use a LocalScript. Additionally, create the Teleporters on the client side to minimize unnecessary touch detections and events from other players. For guidance on optimizing touch events, check out this resource: Optimizing Touch Events in Roblox.

Here’s a step-by-step guide tailored to your task:

  1. Begin by creating Teleporter pairs, each consisting of a Teleporter and a Destination. Organize these pairs under a Folder within your workspace. This structure allows you to manipulate individual parts without affecting others, providing flexibility in positioning.

  2. Put all the Teleporters together into a LocalScript, treating them as a collection. This simplifies management.

  3. Place the LocalScript ideally under the StarterPlayerScripts service for client-side execution.

  4. Within the LocalScript, employ a ‘for’ loop to iterate through all the children (Teleporters) in the collection. For each Teleporter, establish a connection to the ‘Touch’ event and associate it with a callback function. This function should adjust the player’s character position to match the location of the corresponding Destination part. Be certain to access the character’s position through Character.HumanoidRootPart or Humanoid.RootPart, not solely Humanoid.

By adhering to these steps, players will seamlessly teleport between Teleporters and their corresponding Destinations."

-- Get the Players service and the local player
local Players = game:GetService("Players")
local localPlayer: Player = Players.LocalPlayer

-- Retrieve the player's character or wait for it to be added
local character: Model = localPlayer.Character or localPlayer.CharacterAdded:Wait()

-- Get the Humanoid and RootPart of the character
local humanoid: Humanoid = character:WaitForChild("Humanoid")
local rootpart: BasePart = character:WaitForChild("HumanoidRootPart")

-- Get all the Teleporter objects in the script's children
local Teleporters = script:GetChildren()

-- Loop through each Teleporter
for _, teleporter: Instance in Teleporters do
	-- Get the destination CFrame from the 'Destination' child
	local destination = teleporter.Destination.CFrame

	-- Connect a function to the 'Touched' event of the Teleporter
	teleporter.Teleporter.Touched:Connect(function(touchedPart: BasePart)
		-- Check if the touched part is a descendant of the character
		if touchedPart:IsDescendantOf(character) then
			-- Teleport the character to the destination with a vertical offset
			character:PivotTo(destination + Vector3.new(0, humanoid.HipHeight, 0))

			-- Reset the character's velocity to zero
			rootpart.AssemblyLinearVelocity = Vector3.zero
		end
	end)
	
	teleporter.Parent = workspace
end

-- Connect a function to handle character re-spawning
localPlayer.CharacterAdded:Connect(function(addedCharacter: Model)
	-- Update the character, humanoid, and rootpart references
	character = addedCharacter
	humanoid = addedCharacter:WaitForChild("Humanoid")
	rootpart = addedCharacter:WaitForChild("HumanoidRootPart")
end)

image

Telepoters.rbxl (54.8 KB)

Hi there, there may have been a misunderstanding. I want the player to teleport to a certain position(not a part’s position) depending on which part they touch. Ex: If a player touches Trainpart1 or Trainpart3. they will be teleported to the same area, however when they touch Trainpart2 I want the player to be teleported to a different position depending on which Trainpart they touched.
Screen Shot 2023-09-21 at 9.31.35 PM
(Also, I have the script in StarterPlayerScripts but the folder is in the workspace)

Edit: Sorry for the late reply! :sob: