Lighting Script Not Working Properly

Hello!

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()

Can you show the script location in the Explorer, along with the location of all the lights?

1 Like

image

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)

So I should use that for each script?

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 appears to have worked! I’ll try on the other lights.

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.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.