Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can you make a model a table?

Asked by 8 years ago

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.

2 answers

Log in to vote
0
Answered by 8 years ago

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.

0
o I was doing targetModel[index] for some reason, thanks for the clarification! dragonkeeper467 453 — 8y
0
Explain please! User#5978 25 — 8y
Ad
Log in to vote
3
Answered by 8 years ago

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
0
I gave you a rep both answers were great, lunate answered first though... dragonkeeper467 453 — 8y

Answer this question