How to select children already selected by getchildren?

I am trying to create a “blackout” script that will select all parts in a folder, then select all light sources and disable them. How would I be able to select the lights inside of each part?

Here is the script I tried making. It fails at line 3.

local Parts = script.Parent:GetChildren()

local Children = Parts:GetChildren()

for i, v in pairs(Children) do
	wait()
	if v:IsA("PointLight") or v:IsA("SpotLight") then
		v.Enabled = false
	end
end

Just use getdescendants instead.

This assumes of course that lights are only in parts.

1 Like

You can’t get a table of children from Parts as Parts isn’t an instance, it’s also a table of children. Instead, inside of the loop iterating through Parts, define Children = v:GetChildren() and iterate it with a sub-loop.

local Folder = script.Parent

local function ToggleLights(Boolean)
	for _, Child in ipairs(Folder:GetChildren()) do
		if Child:IsA("BasePart") then
			local Light = Child:FindFirstChildWhichIsA("Light")
			if Light then
				Light.Enabled = Boolean
			end
		end
	end
end

Going off the assumption that you have a folder of BasePart instances and within those parts are light instances.

Call the function and pass true or false to it in order to toggle the lights.

Worked perfectly, but it broke after I tried putting it inside a function.

Shouldn’t make a difference, can I see the code?

I cant log onto studio right now, but I will send it the first chance I get.