Trying to make a click detector button for different types of lights. All of the lights are billboard gui's, but I am just trying to enable and disable to shut them off. Also the lights are all individual. I feel like I'm missing a variable. Help please?
`` local isOn = false function on() for i = 1, 18 do local light = script.Parent["RedLight" .. i].RedLight.Enabled isOn = true end function off() for i = 1, 18 do local light = script.Parent["RedLight" .. i].RedLight.Enabled isOn = false end function onClicked() if isOn == true then off() else on() end end script.Parent.ClickDetector.MouseClick:connect(onClicked) on()
I'm not completely sure what you're trying to accomplish.. but from the looks of it, you want to toggle a certain something whenever the ClickDetector is clicked...
If yes, you can simply make use of the if-else statement
, which would allow you to execute code if a condition is true, or another piece of code if not true (false/nil).
All you would have to do is set up a function to toggle a pre-set boolean value, and using whatever said value for your works.
local status = false -- assigning `status` to `false` local toggle = function() status = not status -- toggling boolean value to its opposite. -- false would become true and vice versa if status then -- if true then print(status) -- true for index = 1, 18 do local light = script.Parent["RedLight"..index].RedLight.Enabled end else -- if not true (false/nil) then print(status) -- false for index = 1, 18 do local light = script.Parent["RedLight"..index].RedLight.Enabled end end end script.Parent.ClickDetector.MouseClick:connect(toggle)