This was working earlier, but suddenly stopped. Any help? I also get this error: 15:06:06.860 - SpotLight is not a valid member of SurfaceGui 15:06:06.861 - Script 'Workspace.MainLights.Script', Line 15 15:06:06.861 - Stack End 15:06:06.861 - Disconnected event because of exception
open = script.Parent.Open.Value local lights = {} lights = game.Workspace.MainLights:GetChildren() function operate() if open == false then for _,v in pairs(lights) do v.SpotLight.Enabled = true script.Parent.BrickColor = BrickColor.new(37) open = true wait() end else for _,v in ipairs(lights) do v.SpotLight.Enabled = false script.Parent.BrickColor = BrickColor.new(21) open = false wait() end end end script.Parent.ClickDetector.MouseClick:connect(operate)
Try this.
if open == false then for _,v in pairs(lights) do -- (changed in pairs to in ipairs)
... rest of the script
It is saying that Spotlight doesn't actually exist for each member of the table that is returned from ...MainLights:getChildren()
. So, to fix this error, we need to check and see if a spotlight actually exists in each entry.
open = script.Parent.Open.Value local lights = {} lights = game.Workspace.MainLights:GetChildren() function operate() if open == false then for _,v in pairs(lights) do if v:findFirstChild("SpotLight") then -- Checks to see if a spotlight exists v.SpotLight.Enabled = true script.Parent.BrickColor = BrickColor.new(37) open = true wait() end end else for _,v in ipairs(lights) do if v:findFirstChild("SpotLight") then v.SpotLight.Enabled = false script.Parent.BrickColor = BrickColor.new(21) open = false wait() end end end end script.Parent.ClickDetector.MouseClick:connect(operate)