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

FindFirstChild() function used properly?

Asked by 8 years ago

I'm trying to "turn off" all my lights at the press of a TextButton I used FindFirstChild() and I set the recursive step to true but my lights Enabled property is still true and its material does not change. Thought strangely is runs without errors and runs fully until the printing. Any and all help is appreciated.

script.Parent.PowerOff.MouseButton1Click:connect(function()
    local Lights = workspace.AllLights:FindFirstChild("SpotLight", true)
    Lights.Enabled = false
    Lights.Parent.Material = Enum.Material.SmoothPlastic
    print("Power is off")
end)

1 answer

Log in to vote
3
Answered by 8 years ago

You are misinterpreting the second optional argument for FindFirstChild which will only search the children's children if an instance is not found the first time around.

You will need a loop to set each spotlight instances properties:-

for I,v in pairs(workspace.AllLights:GetChildren()) do
    -- I is the index of the item in the array
    -- v is the object which you will need to set the properties
    --e.g.
    v.Enabled = false
end

If you do not have a model, folder or another form of container to group all lights then you will need to use Recursion to search for all 'SpotLignt' instances.

Hope this helps

0
Thank you so much! lacikaturai 89 — 8y
Ad

Answer this question