I’ve encountered an issue while installing lights in a building I’m working on. I have been using a free model light with a switch so that it can turn on and off. (I installed about 5 of these).
The problem is, when I hit the light switches, only one of the lights turns on/off, the rest do nothing, no matter what switch I click. Is there a fix to this?
Script:
local isOn = true
function on()
isOn = true
game.Workspace.LightSwitch.Light.PointLight.Enabled = true
script.LightSwitchOn:Play()
end
function off()
isOn = false
game.Workspace.LightSwitch.Light.PointLight.Enabled = false
script.LightSwitchOff:Play()
end
function onClicked()
if isOn == true then off() else on() end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
on()
local isOn = true
local descendants = pathToModelOfLights:GetChildren()
function on()
isOn = true
for _, light in pairs(descendants) do
if light:IsA("PointLight") then
light.Enabled = true
end
end
script.LightSwitchOn:Play()
end
function off()
isOn = false
for _, light in pairs(descendants) do
if light:IsA("PointLight") then
light.Enabled = false
end
end
script.LightSwitchOff:Play()
end
function onClicked()
if isOn then off() else on() end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
local isOn = true
function on()
isOn = true
script.Parent.Parent.Light.PointLight.Enabled = true
script.LightSwitchOn:Play()
end
function off()
isOn = false
script.Parent.Parent.Light.PointLight.Enabled = false
script.LightSwitchOff:Play()
end
function onClicked()
if isOn == true then off() else on() end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
on()
It didn’t work because you were accessing the lights from the workspace. Since there were multiple instances of the same name, it randomly chose one light to turn on and off.