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.
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