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

GetChildren method help?

Asked by
JJ_B 250 Moderation Voter
9 years ago

This is part of a script I'm making, it it SUPPOSED to turn lights off when the power goes off. Here is the script:

local p = game.Workspace.Lighting:GetChildren()
        for i = 1,#p do
            p.PointLight.Enabled = false
        end 

However, this doesn't work. Help?

1 answer

Log in to vote
2
Answered by 9 years ago

First, make sure that every instance in workspace.Lighting contains a PointLight. If your script encounters one that does not, it will fail. Also, you may want it to be a recursive function in case the light isn't located directly inside the object in Lighting.

This code will loop through every instance in workspace.Lighting and turn off any PointLights:

function LightsOff(Instance)
    for i,v in pairs(Instance:GetChildren()) do
        if v:IsA("PointLight") then v.Enabled=false end
        LightsOff(v)
    end
end

LightsOff(workspace.Lighting)
Ad

Answer this question