Is there any way to use two or more parts in a touched event?

:wave:

I was just wondering is there any effective way to use two or more parts in a touched event?

For eg.
I have part1 and part2
So if a player touches part1 or part2 a function will connect

part1.Touched:Connect(function(hit)
-- code
end
part2.Touched:Connect(function(hit)
-- code
end

Simply I mean like mixing these two script together.
Sorry if I am not making sense.

You can connect the same function to both events if that’s what you’re trying to do like so:

local function onTouched(part)
	
end

part1.Touched:Connect(onTouched)
part2.Touched:Connect(onTouched)

If you’re trying to connect a lot more than 2 events, you can create a table, iterate through it, and in each iteraction, connect the part’s event to the predefined function:

local Parts = workspace:GetChildren()

local function onTouched(part)
	
end

for i,v in pairs(Parts) do
	if v:IsA("BasePart") then
		v.Touched:Connect(onTouched)
	end
end
6 Likes

The touched event are separate between parts. The only way to do it is to connect both to the same function like @metryy showed.

1 Like

Oh wow something is wrong with me.
I havent used touched event like this in a while.
Thank you