Hi,
So I have this script. It turns all the lights on and off when I click it on and off. Now I want it to randomly strobe the lights. I press the button and it does random strobe lights but not at the same time, different times to make the show effect. Then when I press the button there it all turns off. The button turns RED when on The button turns GREY when off.
Please help to make it work for this script.
local P = script.Parent.Parent local LP1 = P.SpotLight12.Light.SpotLight local LP2 = P.SpotLight11.Light.SpotLight local LP3 = P.SpotLight10.Light.SpotLight local LP4 = P.SpotLight9.Light.SpotLight local LP5 = P.SpotLight8.Light.SpotLight local LP6 = P.SpotLight7.Light.SpotLight local LP7 = P.SpotLight6.Light.SpotLight local LP8 = P.SpotLight5.Light.SpotLight local LP9 = P.SpotLight4.Light.SpotLight local LP10 = P.SpotLight3.Light.SpotLight local LP11 = P.SpotLight2.Light.SpotLight local LP12 = P.SpotLight1.Light.SpotLight lp = {LP1, LP2, LP3, LP4, LP5, LP6, LP7, LP8, LP9, LP10, LP11, LP12} btn = script.Parent.ClickDetector function onClick() for i, v in pairs(lp) do v.Enabled = not v.Enabled end end btn.MouseClick:connect(onClick)
Thanks Kieran
We add a loop in conjunction with the click.
Instead of changing the enabled
state of the lights directly in the click, we just set whether or not the lights are 'on' (strobing) or 'off' (disabled).
A loop works constantly; if the lights are 'off' is disables them (false
) and if they are 'on' (strobing) then it invets them (Enabled = not Enabled
)
local lightsOn = false; function off() for i, v in pairs(lp) do v.Enabled = false; end end function onClick() -- Instead of changing lights directly, -- we just change one variable meaning "on" -- of "off" (where "on" actually means flicker) lightsOn = not lightsOn; if not lightsOn then -- If we have something like a 1 second delay, -- the loop can't turn the lights off until the -- next delay. -- That would make it appear unresponsive, -- so we directly turn it off here. off(); end end btn.MouseClick:connect(onClick); while wait(0.5) do -- Just work every half second if lightsOn then -- If the light switch is enabled, flicker for i, v in pairs(lp) do v.Enabled = not v.Enabled; end else -- If the light switch is set to off, -- keep all the lights off off(); end end
Note that lights flashing at more than 1Hz can do damage to people especially with epilepsy