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.
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