Having trouble with getting decendants

Trying to select a decendant of multiple parts.

So its just not working and the error message is “Workspace.Ship.RoofLights.Script:5: attempt to index nil with ‘Enabled’”

Tried looking for solutions on the forum but it seems no one else has had this problem or has found a way around it. If you do know the way around please let me know.

So I have a Model inside a folder with parts that have surface lights in them and what im trying to do is Disable all the lights at once with a button.

Parts = script.Parent:GetDescendants()
Lights = Parts.SurfaceLight

function off()
	Lights.Enabled = false
end

script.Parent.Button.ClickDetector.MouseClick:Connect(off)

GetDescendants() returns a table with all of the descendants. However, indexing one thing wont give you all the instances with that name. Also, indexing the table with names wont work anyways. If you want to deactivate all of them at once, you’d have to make a for loop that goes through all of the descendants and turns them off if they are a light. Something like this:

Parts = script.Parent:GetDescendants()

function off()
	for i, Light in pairs(Parts) do
		if Light:IsA("SurfaceLight") then
			Light.Enabled = false
		end
	end
end

script.Parent.Button.ClickDetector.MouseClick:Connect(off)

you might wanna do some research on GetDescendants() and tables in general

1 Like

Yea I prob should :skull: I mostly build so I dont have much knowledge on scripting.

Thanks