Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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! :)

6 comments

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.

0

You're right, edited the code to add that. Thanks.

1

Thanks for a great answer! However, I want to understand what "_, child in pairs()" means? Everything else makes sense to me.

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.

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.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.