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

Is there any way to make toggleable lights?

Asked by 8 years ago

Does anybody know how to make toggleable lights with only 1 switch? I tried to make a script to make headlights on my car but it only turns on and stays like that. I want it to turn on and off with only 1 button.Here is my script:

local button = script.Parent
local LightA = script.Parent.Parent.PointLight
local LightB = script.Parent.Parent.Parent.LightB.PointLight
local LightC = script.Parent.Parent.Parent.LightC.PointLight
local LightD = script.Parent.Parent.Parent.LightD.PointLight

button.MouseClick:connect(function()
    LightA.Brightness = '10'
    LightB.Brightness = '10'
    LightC.Brightness = '10'
    LightD.Brightness = '10'
end)
button.MouseClick:connect(function()
    LightA.Brightness = '0'
    LightB.Brightness = '0'
    LightC.Brightness = '0'
    LightD.Brightness = '0'

(LightA,B,C,and D are the parts)

1
Don't take my word for it, but instead of using the Brightness, you could use disable and enable. I forgot if you could actually use Enable/Disable on lights, but go ahead and try. KordGamer 155 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago

Heyo, Normally, to make toggle buttons you gotta do this

local button = mrbutton -- your little button
local light = light -- your light 
local lights_are_on = false -- take a boolean value for the state of the light

button.MouseClick:connect(function()    -- Assuming that you have setup the Click Detector 
    if not lights_are_on then -- check if the lights are on or not on
            light.Enabled = true --  if they are not on, turn them on!
            lights_are_on = true -- Don't forget to check out litttle boolean that helps us alot!
    elseif lights_are_on then
            light.Enabled = false--  if they are on, turn them off!
            lights_are_on = false -- so we know that they are off
    end
end)

Pweh, That was easy right? now what? Well, What if someone presses it alot? omg thats just gonna trigger it soo worse that you will hate it so What do we do now?

We use Debounce! :D http://wiki.roblox.com/index.php?title=Debounce

0
Use an else on line 9. Not an elseif :P Just one of those things. This is just because you don't need to check twice. If it's not the first check it has to be the second one. So there's no use in checking the value twice :P User#11440 120 — 8y
0
ye aye buoyantair 123 — 8y
0
Thanks for your help! I did it! KyofuOmo 25 — 8y
0
It's alright! buoyantair 123 — 8y
Ad
Log in to vote
-2
Answered by 8 years ago

try local table = {LightA, LightB, LightC, LightD}

button.MouseClick:connect(function()
    for i = 1, #table do
        if table[i].Enabled then
        table[i].Enabled = false
        else
        table[i].Enabled = true
        end
    end
end)

Answer this question