What is the easiest way to handle all the portals?

Suppose I have around 10 portals that each teleport you somewhere unique. Here’s the method I’m currently using for each portal:

local Teleport = script.Parent.Teleport
local Arrive = script.Parent.Arrive

Teleport.Touched:Connect(function(hit)

	local HumanoidRootPart = hit.Parent:FindFirstChild("HumanoidRootPart")

	if HumanoidRootPart then

		HumanoidRootPart.CFrame = Arrive.CFrame + Vector3.new(0, 4, 0)

		TeleportCanTouch = false

		task.wait(1)

		Teleport.CanTouch = true

	end

end)

Very simple method that works well. But I don’t want to use multiple scripts for each portal. I want to use one script that handles all the portals. But I can’t think of the best way to do this without copying and pasting this code for each portal in the script.

Question: How can I use one script to handle all the portals while keeping the code short? I want a Folder in Workspace with all the portals and the script in the Folder as well.

Probably an attribute or value for each portal of where it’s going to teleport, or a module. Then probably make a for loop for the portals with a touched event, and teleport them to the vector in the value or the index in the module.

1 Like

Very simple example but this works:

local folder = workspace:FindFirstChild("YourFolder") -- Name of your folder

for _, portal in ipairs(folder:GetChildren()) do

   local Teleport = portal.Teleport
   local Arrive = portal.Arrive

   Teleport.Touched:Connect(function(hit)

   	local HumanoidRootPart = hit.Parent:FindFirstChild("HumanoidRootPart")

   	if HumanoidRootPart then

   		HumanoidRootPart.CFrame = Arrive.CFrame + Vector3.new(0, 4, 0)

   		TeleportCanTouch = false

   		task.wait(1)

   		Teleport.CanTouch = true

   	end

   end)

end

That gets the children of the folder and finds the teleport and arrive and then uses my code but this wouldn’t work for multiple portals

Put the models in a folder with each teleporter model having a teleport and arrive part

1 Like

Why not?

character fillllllllllllllllllllllllllllllllllll

If I wanted ChatGPT to help me I would have went there instead

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