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

Is there any way to make toggleable lights?

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 comment

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.

2 answers

✓ Accepted answer

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

4 comments

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

0

ye aye

0

Thanks for your help! I did it!

0

It's alright!

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)

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