Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a clickable lightbar, what am I doing wrong?

Asked by 7 years ago

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)

1 answer

Log in to vote
0
Answered by 7 years ago

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)

0
Thanks ! bromawsomusryanadam -1 — 7y
0
It says that there is an error at : script.Parent.ClickDetector.MouseClick:connect(onClicked) bromawsomusryanadam -1 — 7y
0
Okay I figured it out. You were supposed to add another end at line 49. Thanks so much for your help :D bromawsomusryanadam -1 — 7y
0
Oh whoops, sorry about that! Your welcome! User#17125 0 — 7y
Ad

Answer this question