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 4 years ago
Edited 4 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)

local parts = game.Workspace.Model:GetChildren()
local power = game.Workspace.Value

while true do
    wait(1)
    if power.Value > 0 then
    power.Value = power.Value - 1
    if power.Value == 0 then
        for i, v in pairs(parts) do
            v.Material = Enum.Material.SmoothPlastic
        end

        else
            for i, v in pairs(parts) do
            v.Material = Enum.Material.Neon
            end
    end
end
end

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 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.

local descendants = game.Workspace.Model:GetDescendants()
local power = game.Workspace.Value

while true do
    wait(1)
    if power.Value > 0 then
        power.Value = power.Value -1
    elseif power.Value == 0 then
        for i=1, #descendants do
            if descendants[i]:IsA("BasePart") then
                descendants[i].Material = "SmoothPlastic"
                if descendants[i].PointLight then
                    descendants[i].PointLight.Enabled = false
                end
            end
        end
    else
        for i=1, #descendants do
            if descendants[i]:IsA("PointLight") then
                descendants[i].Parent.Material = "Neon"
                descendants[i].Enabled = true
            end
        end
    end
end

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

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

Answer this question