Best way of connecting teleporters while following DRY?

Lately I’ve been coding my teleporters to teleport to a specific teleport when you click on another and then adding a script into it. Is there a way of connecting teleporters and only using 1 script? I was thinking of a system like how a beam gets attached to 2 different models via, attachments but I don’t know how to properly make that work.

1 Like

Could you rephrase the question? This is what I get from your post:

  1. You have 2 or more teleporters
  2. You click on teleporter 1 and get sent to the teleporter 2 position

I don’t understand what you mean by adding a script. Do you mean that when you teleport to the teleporter 2 position, you add a new teleport script to teleporter 2?

2 Likes

By putting 2 portals into a Model or a Folder you can easily access any of them. Let’s say there are 2 parts in the Model called Teleporters and you put the script there.

local Teleporters = script.Parent
local t1 = Teleporters.t1 --first teleporter part
local t2 = Teleporters.t2 --second teleporter part
local db = false

local function tot1(part)
	local humanoid = part.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid and db == false then
		db = true
		part.Parent.HumanoidRootPart.Position = t1.Position + Vector3.new(0,1,0)
		wait(0.5)
		db = false
	end
end

local function tot2(part)	
	local humanoid = part.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid and db == false then
		db = true
		part.Parent.HumanoidRootPart.Position = t2.Position + Vector3.new(0,1,0)
		wait(0.5)
		db = false
	end
end

t1.Touched:Connect(tot2)
t2.Touched:Connect(tot1)

You can add more parts to the script and easily direct the start and end location
Example with 3 teleporters:

3 Teleporters
local Teleporters = script.Parent
local t1 = Teleporters.t1 --first teleporter part
local t2 = Teleporters.t2 --second teleporter part
local t3 = Teleporters.t3 --third
local db = false

local function tot1(part)
	local humanoid = part.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid and db == false then
		db = true
		part.Parent.HumanoidRootPart.Position = t1.Position + Vector3.new(0,1,0)
		wait(0.5)
		db = false
	end
end

local function tot2(part)	
	local humanoid = part.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid and db == false then
		db = true
		part.Parent.HumanoidRootPart.Position = t2.Position + Vector3.new(0,1,0)
		wait(0.5)
		db = false
	end
end

local function tot3(part)	
	local humanoid = part.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid and db == false then
		db = true
		part.Parent.HumanoidRootPart.Position = t3.Position + Vector3.new(0,1,0)
		wait(0.5)
		db = false
	end
end

t1.Touched:Connect(tot2)
t2.Touched:Connect(tot3)
t3.Touched:Connect(tot1)

I suppose it’s not technically DRY, but it is still one script and further improvements can be made.

1 Like

Adding onto this, if you want DRY, you can do the following:

local Teleporters = script.Parent
local t1 = Teleporters.t1 --first teleporter part
local t2 = Teleporters.t2 --second teleporter part
local db = false

local function Teleport(teleportToObject, partToTeleport)
	local humanoid = partToTeleport.Parent:FindFirstChildWhichIsA("Humanoid")
	if humanoid and db == false then
		db = true
		partToTeleport.Parent.HumanoidRootPart.Position = teleportToObject.Position + Vector3.new(0,1,0)
		wait(0.5)
		db = false
	end
end

t1.Touched:Connect(function(partToTeleport)
    Teleport(t2, partToTeleport)
end)

t2.Touched:Connect(function(partToTeleport)
    Teleport(t1, partToTeleport)
end)

Note: Above code has not been tested.

1 Like

I believe that might be better. Thanks for DRYing it up :sweat_smile:

3 Likes

Whoops! Lets say I want to connect 2 teleporters the teleports the player to each other, normally I’d make a script in both of defining where the other teleporter is, my question is if there would be a way to make the teleports be able to teleport to each other without needing 2 scripts in both. and if possible, maybe even without defining the teleports without editing the script.

Adding onto myself, specifically a way to avoid a mess that looks like this


A big hassle to adding EVERY teleporter in the game into one singular script then a function for every connection

You can easily solve this by creating an attribute named “destination” (or whatever you like) inside each teleporter that stores the name of the destination teleport, after that, all you have to do is retrieve its value, search for the target teleporter inside the folder, and teleport the player. Here’s an example of how to do it:

local folder = -- Teleporters folder.

local function useTeleport(player, teleporter)
	local destination = teleporter:GetAttribute("destination")
	local targetPort = folder:FindFirstChild(destination)

	if targetPort then
		-- Teleport the player
	end
end

for _, teleporter in ipairs(folder:GetChildren()) do
	teleporter.Touched:Connect(function(part)
		local player = -- Get the player that touched the teleporter.
		useTeleport(player, teleporter)
	end)
end
2 Likes