How can i get all of the children in a part and affect their properties?

ABOVE ALL ELSE: this is a future thing I’m going to have to encounter soon, so i want a code to do so before i run into the issue.

I need all of children of a part gathered, then affect said children(s) properties. help?

2 Likes

To get all Children within an Instance, you would use Instance:GetChildren(), This will return an array (a table) of Instances.

local Children = Part:GetChildren() -- Gets Children
print(Children) -- returns table

A More Powerful one being Instance:GetDescendants() which gets the Instances Children and their Childrens:

local Children = Part:GetDescendants() -- Gets All Children
print(Children) -- returns table of Everything Descending from an Instance

However you would use a for loop to iterate over all the Instances within that List, going through it manually would suck

for index, Instance in pairs(Part:GetChildren()) do -- Iterates through a List of Items
    print(index, Instance) -- prints number, and Item
    Instance.Transparency = 1 -- Changes Properties of Instances
end

However, you would need to type check if the Instance is what you want:

if Instance:IsA("BasePart") then -- Checks if the Instance is a Part

If the loop iterates through an Item that doesn’t have the Property, it will error.

5 Likes

great, we get the children, now how do we affect the properties of all the gathered children?

edit: i sent this right as you edited your message

You can get the children of a part one by one by for-looping through them:

local Part = --part you want to get the children from

for index, child in pairs(Part:GetChildren()) do
    child.Transparency = 0 --change any property of the child
end

or what @DasKairo suggested:

local Part = --part you want to get the children from
local Children = Part:GetChildren() -- Gets Children
print(Children) -- returns table

The first option would be shorter and simpler in my opinion.

it works, but errors when it tries to change the color of the script, so i tried applying this script:

if Instance:IsA("BasePart") then -- Checks if the Instance is a Part

from @DasKairo but that gives another error:

without that code:
image

Could you show the full script?

with or without @DasKairo’s code?

All of the code will be required to debug it

Just send me the script you have right now.

That’s what I meant by this, your code should look like this (if you did this):

for index, Instance in pairs(Part:GetChildren()) do -- Iterates through a List of Items
    print(index, Instance) -- prints number, and Item
    if Instance:IsA("BasePart") then
        Instance.Transparency = 1
    end
end
1 Like

i deleted her code after it errored

it runs into the script and tries to change its color, but ofcourse that isnt a property, heres the diagram of all the children
image

can i remove the “print(index, instance)” part?

Add a check to see if it is a basepart

Yes, its just a debug

Already said it…

I know. I’m telling him to add it again because there is no way “child” is a table.

i found this to work with no problems, but i tested this on a low-level scale, and it might not be able to get the children of models as well (remember, it was a low-level scale, and i still need to implement this into the actual game)

just tested it, it does not change everything inside a model, only specifically parts

image

model just has 2 parts inside of it