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

How do I get children of GetChildren()?

Asked by 5 years ago
Edited 5 years ago

Hello, I would like to know how I could get the children of a GetChildren()

In my game, I have a model that contains parts containing pointlights. What I would like to do is set PointLight.Enabled to false using a for loop, however I am unsure how I would reference all of these pointlights?

Here is my current script:

(The model containing the parts that contain pointlights is "Model" - top line)

01local parts = game.Workspace.Model:GetChildren()
02local power = game.Workspace.Value
03 
04while true do
05    wait(1)
06    if power.Value > 0 then
07    power.Value = power.Value - 1
08    if power.Value == 0 then
09        for i, v in pairs(parts) do
10            v.Material = Enum.Material.SmoothPlastic
11        end
12 
13        else
14            for i, v in pairs(parts) do
15            v.Material = Enum.Material.Neon
16            end
17    end
18end
19end

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

GetChildren() returns only the first children of a parent. GetDescendants(), on the other hand, returns the children of the children as well. If you want to use the value like a timer which turns lights off when it reaches 0, and whenever someone makes it a negative value the lights come on, try this.

01local descendants = game.Workspace.Model:GetDescendants()
02local power = game.Workspace.Value
03 
04while true do
05    wait(1)
06    if power.Value > 0 then
07        power.Value = power.Value -1
08    elseif power.Value == 0 then
09        for i=1, #descendants do
10            if descendants[i]:IsA("BasePart") then
11                descendants[i].Material = "SmoothPlastic"
12                if descendants[i].PointLight then
13                    descendants[i].PointLight.Enabled = false
14                end
15            end
View all 25 lines...

If you simply want a loop to turn off any PointLights in a model, try this:

1local descendants = game.Workspace.Model:GetDescendants()
2for i=1, #descendants do
3    if descendants[i]:IsA("PointLight") then
4        descendants[i].Enabled = false
5    end
6end
Ad

Answer this question