My script doesn't work

I want to disable all lights. The hierarchy is simple. Workspace > Facility > Roof Lamp > Light > PointLight.
image

My script is:

local function turnOffLights()
	local roofLamp = game.Workspace.Facility["Roof Lamp"].Light

	for i, child in pairs(roofLamp:GetChildren()) do
		if child:IsA("PointLight") then
			child.Enabled = false
		end
	end
end

I think it’s supposed to work, but it doesn’t :frowning:
I’ve did some testing and everything works correctly, it finds the workspace, the folder and then the light. So hierarchy is correct. I really don’t know what is wrong and this is driving me crazy.

Is this a server-side Script or a client-side LocalScript? Were any errors thrown?

Server side, no errors everything works seamlessly except the lights dont turn off

You have used GetChildren(), which only gets the children. Use GetDescendants().

With that being said, it would be more efficient to just do:

roofLamp.Light:GetChildren()

or

roofLamp.Light.PointLight

Are you calling the function properly? Can you please send the part of your code that calls that function?

Im sure it does work because I get printed “Worked”, just a thing i added in function for debug

There is a lot of Roof Lamps, and I need to get them all and in all of them I need to disable the lights

Are all of the models (containing the PointLights that you need to disable) under the Facility folder?

Yes they do, I can double check if you want

In that case, you would do something like the following:

local folder = game.Workspace.Facility

for i, v in pairs(folder:GetDescendants()) do
    if v:IsA("PointLight") then
        v.Enabled = false
    end
end
2 Likes

Let me try real quick…

It worked! Thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.