Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

4 comments

0

Thanks !

0

It says that there is an error at : script.Parent.ClickDetector.MouseClick:connect(onClicked)

0

Okay I figured it out. You were supposed to add another end at line 49. Thanks so much for your help :D

0

Oh whoops, sorry about that! Your welcome!

This discussion is preserved as it was. New answers, comments, and votes are closed.