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

How to Get All Children and Change Them?

Asked by 9 years ago

Okay, So I made a script that when one block is hit by a part called 'Rocket' it changes the child (removes it or changes material)

It works with one or two chains (children) but when I have many of them it's hard to list them out in a script and it gets complicated.

How can I make it so it gets the children and then changes them as a whole?

:GetChildren() only gives a read-only output, so how can I accomplish this?

Script that works with two chains:

local buttonPressed = false

Hits = script.Parent.Hits
Chain1 = script.Parent.Parent.Chain1
Chain2 = script.Parent.Parent.Chain2

function checkhits()
    Hits = Hits
end


script.Parent.Touched:connect(function(bang)
    if not buttonPressed then
        buttonPressed = true
    if bang.Name == "Rocket" then
        Hits.Value = Hits.Value + 1
        wait(1)
        buttonPressed = false
    end
    end
end)





        while true do
        wait(0.1)
        checkhits()
        if Hits.Value == 1 then
            Chain1.Material = "Marble"
            Chain2.Material = "Marble"
        elseif Hits.Value == 2 then
            script.Parent:Destroy()
            Chain1.Material = "CorrodedMetal"
            Chain2.Material = "CorrodedMetal"
            Chain1.Anchored = false
            Chain2.Anchored = false
        end
        end

2 answers

Log in to vote
1
Answered by
Unclear 1776 Moderation Voter
9 years ago

If you want to get all children, you would use the GetChildren method like you said.

GetChildren is a method that returns a table containing all of the children of the object it is used on. For example, workspace:GetChildren() would return a table full of all of the children of workspace.

We can use this to our advantage if we want to do something to all of the children of an object by iterating through the table. Since the table is in list form, that means that all indices are numerical, and that allows us to use a simple for loop with an increment variable or we can use a key-value pair iterator function like pairs or next.

Now, we can get a reference to each child of the table and do something to it.

For loop with increment variable

local children = workspace:GetChildren()
for index = 1, #children do
    local child = children[index]
    -- do whatever you want to this child
end

For loop with iterator function next

for _, child in next, workspace:GetChildren() do
    -- do whatever you want to this child
end

For loop with iterator function pairs

for _, child in pairs(workspace:GetChildren()) do
    -- do whatever you want to this child
end

If I wanted to print all of the children of workspace with next, I would write the following

for _, child in next, workspace:GetChildren() do
    print(child)
end
Ad
Log in to vote
1
Answered by 9 years ago

Building off of Sukinahito's answer, you might want to build a function to apply the property to all children automatically:

function ApplyProperty(property, value)
    local ch = script.Parent:GetChildren()
    for i = 1, #ch do
        pcall(function() ch[i][property] = value end)
    end
end
--Usage:
ApplyProperty("Anchored", true)

I use pcall incase the child doesn't have the property. Without pcall, you could get an error when you try to run it if one of the children doesn't have the property.

Answer this question