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

How do I get the children of a :GetChildren()?

Asked by 8 years ago

I'm trying to make a script that would unanchor parts inside a model, which is inside another model. Like this:

  • Workspace
    • Model
      • Script (the script)
      • ELights (first Model)
        • EmergencyLight (second Model)
          • Part (the parts i want to unanchor)
          • Part
        • EmergencyLight (second Model)
          • Part (the parts i want to unanchor)
          • Part

To do this, I would use :GetChildren()

c = script.Parent.ELights:GetChildren()

for i=1, #c do
    if c[i].className == ? then
        c[i].Anchored = false
end

Now, the code above is incomplete, as I don't know how to get the children after doing :GetChildren() on the Model which is named "ELights" This is where i ask for help.

0
How did you make the boxes and circles? woodengop 1134 — 8y

4 answers

Log in to vote
-2
Answered by 8 years ago

You've got the children of your first model "ELights", you would just do another for loop inside the first for loop to get the children in the other models too (or grandchildren, depending on what you want to call them).

Since you're using a numeric for loop, I will just keep to using a numeric for loop as well in the below script.

c = script.Parent.ELights:GetChildren()

for i=1, #c do
if c[i].Name == "EmergencyLights" then --In case you only want the items in the model named "EmergencyLights" to be unanchored.
    lights = c[i]:GetChildren() --Get the children of the "EmergencyLights" model.
    for l=1, #lights do --Repeats the loop l times. l will be the number of children in the "EmergencyLights" model.
        lights[l].Anchored = false --Unanchor the part.
    end
end

The reason we don't use i in the next for loop is because i is already being used by the first loop. You may also want to add an if statement before unanchoring to check if the child is a part.

0
It works, thanks! RobloxGuy6403 15 — 8y
1
Did you legit downvote everyone else's answer just to get yours to the top? Selfish cheater. Goulstem 8144 — 8y
0
@Goul I wish i can downvote -.-. That is very selfish of him. FutureWebsiteOwner 270 — 8y
0
Don't worry, I did. Selfish cheater. woodengop 1134 — 8y
View all comments (3 more)
0
What? I didn't down vote anyone's answer. I only posted a solution to his problem and went to do other things. Spongocardo 1991 — 8y
0
I can't believe people think I'm that cruel. I just posted an answer to try and explain to him how to fix his problem and everyone else down votes me because mine was accepted? Pathetic. Spongocardo 1991 — 8y
0
people thought that, wow narrowricky -14 — 3y
Ad
Log in to vote
5
Answered by 8 years ago

There are two types of for loops you can use to loop through all children in the model

for i,k in pairs (model:GetChildren()) do

or

for i=1,#model:GetChildren() do

The first type will be easier to use, but let's start with the second. It seems you understand how the second method works.

model:GetChildren()[1] --child 1 of model
model:GetChildren()[2] --child 2 of model

The second method will use the variable 'k' and change it to the child so you dont have to use [1],[2], etc.

k = model:GetChildren()[n] --when the loop goes through the code the nth time
k = model:GetChildren()[1] --when the loop goes through the code the 1st time

so in your context, this is how the code would look:

local c = script.Parent.ELights:GetChildren()

for i,k in pairs (c) do
    if k.ClassName == "Part" then --I would use k:IsA("BasePart") but you could use this
        k.Anchored = false
    end
end

Ask me for more help if needed.

0
I'm pretty sure when you say "I would use k:IsA("Basepart") but you could use this" You need to say "BasePart" instead of "Basepart". We all know caps matter lightpower26 399 — 8y
0
I don't use :IsA("BasePart") often enough to get that right :P FutureWebsiteOwner 270 — 8y
Log in to vote
1
Answered by
Wizzy011 245 Moderation Voter
8 years ago

In your for loop, each child is represented by c[i], the c counts as all the children that you have, and i is the number of the current child you're unanchoring. If you undid a for loop, and looked at each individual child, the loop would look like this:

i           c
1           A part
2           A different part
3           Another alternative part
4           Once again a different part
5           and so on..

So basically, you've already gotten all the children! If you wanted to find them again after you used this loop, just do the same.

Also, you need another end between line 5 and 6, you've not closed your if statement.

Log in to vote
0
Answered by 8 years ago

What I would do is make a function so that the same code can be run inside the same code again. In case you aren't sure what I mean let me show you:

--I will make two scripts. One that gets all the children of the model and put it into a table, and one that makes all the parts unanchored  

--TABLE CHILDREN SCRIPT

local AllChildren = {} --Make an empty table

function GetEveryChild(a) --Create a function with the argument "a"
    for i,v in pairs(a:GetChildren()) do --i is an integer. It's the "Identification Number" for that object. v is the actual object
        table.insert(AllChildren, #AllChildren+1, v) --I sometimes have problems with this. If it works for you then congratulations
        GetEveryChild(v)
    end
end

GetEveryChild(game.Workspace) --This will fire the "GetEveryChild" function which will completely scan Workspace.

--UNANCHORED SCRIPT

function UNANCHORALLPARTS(b) --Create a function with the argument "b"
    for i,v in pairs(b:GetChildren()) do
        if v:IsA("BasePart") then --If the object is any type of part then...
            v.Anchored = false --Unanchor the part.
        end
        UNANCHORALLPARTS(v)--Scan inside the object for any other parts
    end
end

UNANCHORALLPARTS(game.Workspace.Model) --The script will search for a Model in workspace called "Model" and Unanchor any parts inside.

Answer this question