I've made model and added a bunch of parts inside that model all the same name. I want to check individually if an event occurs on each part inside this model, how can i create a for loop to do this? I've tried but failed to put these parts in a table since they are all the same name. The parts are added to the table in a for loop.
✓ ANSWER ACCEPTED
Can you make a model a table?
2 answers
✓ Accepted answer
Iterate through all of the children in the Model using a for loop
for index, part in ipairs(targetModel:GetChildren()) do
-- part is now each part enjoy.
end
ipairs() takes a table and returns an iterator for use in a generic for loop. It iterates over the array section of the target table, providing the index and value for each key-value pair in the array part of the table.
It is also worth noting that it is about 1.5x as fast as pairs.
The 'GetChildren' method of a model returns a table with all the children of the model.
local children = targetModel:GetChildren() -- a table of all the children
Then you can use it in a for loop like any other table
local children = targetModel:GetChildren()
for i, v in pairs(children) do
end
1 comment
0
I gave you a rep both answers were great, lunate answered first though...
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
o I was doing targetModel[index] for some reason, thanks for the clarification!
Explain please!