When I click my Police LightBar It turns on but when I click it again it won't turn off. What am I doing wrong?
local lightBar = script.Parent local blue = lightBar.BlueLight local blue1 = lightBar.BlueLight1 local red = lightBar.RedLight local red1 = lightBar.RedLight1 local red2 = lightBar.RedLight2 local function turnOn(part) part.Material = Enum.Material.Neon part.SurfaceLight.Enabled = true end local function turnOff(part) part.Material = Enum.Material.Plastic part.SurfaceLight.Enabled = false end local function onClicked(player) while true do turnOn(blue) turnOn(blue1) turnOff(red) turnOff(red1) turnOff(red2) wait(0.5) turnOn(red) turnOn(red1) turnOn(red2) turnOff(blue) turnOff(blue1) wait(0.5) end end script.Parent.ClickDetector.MouseClick:connect(onClicked)
You never coded it to turn off LOL.
Here is a version of the script that uses a variable to determine if the lights are on or off, and breaks the loop of the code ran by the last trigger of the event to stop the lights.
WARNING CODE NOT TESTED
local lightBar = script.Parent local blue = lightBar.BlueLight local blue1 = lightBar.BlueLight1 local red = lightBar.RedLight local red1 = lightBar.RedLight1 local red2 = lightBar.RedLight2 -- NEW STUFF local OnorOff= "Off" local function turnOn(part) part.Material = Enum.Material.Neon part.SurfaceLight.Enabled = true end local function turnOff(part) part.Material = Enum.Material.Plastic part.SurfaceLight.Enabled = false end local function onClicked(player) if OnorOff == "Off" then OnorOff =="On" while true do if OnorOff =="On" then turnOn(blue) turnOn(blue1) turnOff(red) turnOff(red1) turnOff(red2) wait(0.5) turnOn(red) turnOn(red1) turnOn(red2) turnOff(blue) turnOff(blue1) wait(0.5) else break end end else OnorOff = "Off" end script.Parent.ClickDetector.MouseClick:connect(onClicked)