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:
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()
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
if v:IsA("BasePart") then
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.