Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Touched event for multiple parts in a group?

I was wondering if there's a way to run the touched event for multiple parts in a group w/o having to put:

local p1 = script.Parent.Part1
local p2 = script.Parent.Part2
local p3 = script.Parent.Part3

function onTouch()
print("Part was touched")
end

p1.Touched:connect(onTouch)
p2.Touched:connect(onTouch)
p3.Touched:connect(onTouch)

Is there a way to do this w/ a loop? Thanks for any advice!

1 answer

GetChildren

There's a method called GetChildren which returns an array of objects that are parented to the one you called the method on. We can then iterate through this table, to get all the individual objects we want to connect the event to. For this, we use a for loop.

For loops

There are two kinds of for loops: numeric for and generic for (I'll let you read about them in the links provided if you're unfamiliar with them). Since the table GetChildren returns is an array (meaning all of it's keys are in sequential numeric order), we can use either kind of for loop. For the sake of simplicity and readability, I'll give an example using generic for (though traditionally, you'd use a numeric for loop for iterating arrays since they're better at it. But you don't have to worry about that for now).


Step 1: So the first thing we're going to do, is get the children of the object. As previously mentioned, you can do so by simply calling GetChildren on the object:

-- We're going to make pretend "Model" is defined somewhere in the script, and that it represents a model in the workspace full of BaseParts.
local Objects = Model:GetChildren()

Step 2: The next thing we're going to do, is create our for loop. Instead of calling pairs or ipairs for our generic for loop's iterator, I'm just going to use the next function directly. This is optional, and you don't have to worry about what this means for now.

local Objects = Model:GetChildren()

-- "i" being the index in the table we're iterating, and "v" being the value associated with that index. "Objects" is obviously the table we're iterating.
for i,v in next, Objects do
	
end

Step 3: Now to implement your code with what we have done so far. To do this, we'll simply connect the event with each individual part inside the for loop (with "v", since that's our value), and since we're only using it once we don't have to define a function. We'll just pass it anonymously.

local Objects = Model:GetChildren()

for i,v in next, Objects do
	
	-- This is good practice, just to make sure we're only working with
	-- BaseParts in the model.
	if v:IsA("BasePart") then
		
		-- Pass an anonymous function to the new Touched 
		-- event we created for this individual part.
		v.Touched:connect(function(hit)
			print(hit.Name .. " touched the part")
		end)

	end
end

That will make it so all BaseParts inside an object are now connected to the event we gave it in the for loop. If you have any questions on how this works, or on any of the links provided in the explanation?, just let me know.

This discussion is preserved as it was. New answers, comments, and votes are closed.