I've made a code that operates from one button, but it doesn't work, can you identify it?
local light1 = script.Parent.Parent.L1.SpotLight local light2 = script.Parent.Parent.L2.SpotLight function onClicked(mouse) if light1.Enabled == false and light2.Enabled == false then light1.Enabled = true light2.Enabled = true light1.Transparency = 1 light2.Transparency = 1 end if light1.Enabled == true and light2.Enabled == true then light1.Enabled = false light2.Enabled = false light1.Transparency = 0 light2.Transparency = 0 end end script.Parent.ClickDetector.MouseButton1Click:connect(onClicked)
You just made 1 simple mistake, which I have learned from making many times myself. You used the wrong event for the ClickDetector. MouseButton1Click
is an event for GUI Objects, such as a TextButton, whereas the event you are looking for is MouseClick
.
To fix it, we'll simply swap that out with the correct event, and your code should work:
local light1 = script.Parent.Parent.L1.SpotLight local light2 = script.Parent.Parent.L2.SpotLight function onClicked(mouse) if light1.Enabled == false and light2.Enabled == false then light1.Enabled = true light2.Enabled = true light1.Transparency = 1 light2.Transparency = 1 elseif light1.Enabled == true and light2.Enabled == true then --Also I simplified this by switching it to an elseif statement. Feel free to use the code with or without this light1.Enabled = false light2.Enabled = false light1.Transparency = 0 light2.Transparency = 0 end end script.Parent.ClickDetector.MouseClick:connect(onClicked)
And now this should work!
If you have any further problems/questions, please leave a comment below and I'll see what I can do. Hope I helped :P