✓ Accepted answer
Combine both anonymous functions!
Lights = workspace:WaitForChild("LightsModel")
LightsTable = Lights:GetChildren()
isACTIVE = false
script.Parent.MouseButton1Click:connect(function ()
if isACTIVE then
for i, Part in pairs(LightsTable) do
if Part:IsA("BasePart") then
for i, Light in pairs(Part:GetChildren()) do
if Light:IsA("Light") then
Light.Enabled = false
end
end
end
end
isACTIVE = false
else
for i, Part in pairs(LightsTable) do
if Part:IsA("BasePart") then
for i, Light in pairs(Part:GetChildren()) do
if Light:IsA("Light") then
Light.Enabled = true
end
end
end
end
isACTIVE = true
end
end)
Use the generic "for" loop to iterate over every value of the LightsTable array.
:IsA("") is a method that returns the class of the object.
As you can see, I went into extreme measures into checking if there's more than one light objects in a part, just in case, because I do not know what your descendants of LightModel looks like.
This script, of course, can be shortened to increase efficiency.
Lights = workspace:WaitForChild("LightsModel")
LightsTable = Lights:GetChildren()
isACTIVE = false
function Switch(Boolean)
for i, Part in pairs(LightsTable) do
if Part:IsA("BasePart") then
for i, Light in pairs(Part:GetChildren()) do
if Light:IsA("Light") then
Light.Enabled = Boolean
end
end
end
end
isACTIVE = Boolean
end
script.Parent.MouseButton1Click:connect(function ()
if isACTIVE then
Switch(not isACTIVE)
else
Switch(not isACTIVE)
end
end)