I have a few parts, I don’t want to do a touched event for each part.
How is it possible to create a table of the parts and use one touched event that will teleport you to a different location based on what part is touched?
I know it’s probably completely off but this is what I tried:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teleporter = ReplicatedStorage:WaitForChild("Teleporter")
local debounce = false
local purplePortal = workspace.Portal.Purple
local cyanPortal = workspace.Portal2.Cyan
local portals = {purplePortal, cyanPortal}
portals.Touched:Connect(function(hit)
if not debounce then
local isPlayer = Players:GetPlayerFromCharacter(hit.Parent)
if isPlayer then
debounce = true
if portals[1].Touched then
print("Purple")
Teleporter:FireServer(player, "Purple")
elseif portals[2].Touched then
print("Cyan")
Teleporter:FireServer(player, "Cyan")
end
wait(0.5)
debounce = false
end
end
end)
No I didn’t mean a body part, I meant if an actual block in game is touched (doesn’t matter which bodypart) it will teleport the player based on which block/portal it is.
use this instead of doing seperate touched events:
for i,v in pairs(portals) do
v.Touched:Connect(function(hit)
if not debounce then
local isPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
if isPlayer then
print(v.Name)
Teleporter:FireServer(player, v.Name)
end
end
end)
end
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Teleporter = ReplicatedStorage:WaitForChild("Teleporter")
local debounce = false
local purplePortal = workspace.Portal.Purple
local cyanPortal = workspace.Portal2.Cyan
local portals = {purplePortal, cyanPortal}
for i,v in pairs(portals) do
if v:IsA("BasePart") then
v.Touched:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then --makes sure it is a player
print(v.Name)
Teleporter:FireServer(game:GetService("Players").LocalPlayer, v.Name)
end
end)
end
end
You can just use a i,v loop and it will loop through every portal
Loops are your friend! in pairs() works with any table you give it, and is helpful here.
for _,portal in pairs(portals) do -- Runs through all the portals in your 'portals' table
portal.Touched:Connect(function() -- Checks if any of them is Touched.
Teleporter:FireServer(player, portal.Name) -- This is what I guessed you want to happen.
end)
end
Pretty important to note… Just calling this for Touched() will result in the player getting teleporter if any part touches the portal.
You should make sure the player’s character is touching the part.
portal.Touched:Connect(function(Hit)
if Hit.Parent == player.Character then
-- execute command here
end
end)