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

Is it possible to use function :GetChildren() to change all properties of parts under a model?

Asked by
RAYAN1565 691 Moderation Voter
5 years ago

I don't know if this can be done but I tried researching whether it is possible to use the function :GetChildren() to change and set the same property for all parts under a model. How can this be done, if possible?

myModel = script.Parent.Model
myModelParts = myModel:GetChildren()

myModelParts.Anchored = true
myModelParts.Transparency = 0.5
myModelParts.Position = myModelParts.Position+Vector3.new(1,0,1)

1 answer

Log in to vote
2
Answered by
chomboghai 2044 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Yep. Just loop through the children.

local myModel = script.Parent:FindFirstChild("Model")
local myModelChildren = myModel:GetChildren()

for _, child in pairs(myModelChildren) do
    if child:IsA("BasePart") then
        child.Anchored = true
        child.Transparency = 0.5
        child.Position = child.Position + Vector3.new(1, 0, 1)
    end
end

Hope this helps! :)

0
Good answer! Would like to quickly note in case Rayan isn't aware; this code will error if any children in myModel happen to not have the properties 'Anchored', 'Transparency', and 'Position.' Adding an if statement to check, for example, that the child is a basepart will remedy this issue. saenae 318 — 5y
0
You're right, edited the code to add that. Thanks. chomboghai 2044 — 5y
1
Thanks for a great answer! However, I want to understand what "_, child in pairs()" means? Everything else makes sense to me. RAYAN1565 691 — 5y
1
I've also noticed that the for do loop doesn't contain the starting, ending values, and the increment. Maybe you can explain this to clear up the confusion I'm experiencing. RAYAN1565 691 — 5y
View all comments (2 more)
0
Yep! This loop is called a "generic loop". You can loop through a table by saying `for key, value in pairs(table) do`, and key will be the key of the table (aka 1, 2, 3 or if you have custom keys), and value will be the value of that element. So table["Hello"] = 3, the key would be "Hello" and the value would be 3. chomboghai 2044 — 5y
0
There iterative loop is using a number and an increment value, but that's usually used when you know exactly how many times you want to loop. For tables, they are usually sizes that we don't know when we're looping through them (for example, :GetChildren()). chomboghai 2044 — 5y
Ad

Answer this question