0

# Light Switches?

How do I make a on/off button for a point light or a spot light?

2
Shawnyg 4251
7 years ago

Here you go:

on = false
script.Parent.ClickDetector.MouseClick:connect(function(click)
if on == false then
on = true
script.Parent.PointLight.Enabled = true
else
on = false
script.Parent.PointLight.Enabled = false
end
end)


So here is how you should order your parts:

1.Create the switch/brick. 2.Put the PointLight/SpotLight in it. 3.Put a ClickDetector in the brick as well.

-Thank me by accepting this answer/bumping up my reputation!

0

function switch()

w = workspace.whatever if w.Transparency == 0 then w.Transparency == 1 elseif w.Transparency == 1 then w.Transparency == 0 end end

--connection line

0
how do you do that lua format :( Yumeragon 45 — 7y
0

I did it similar to Shawnyg, but I used the range value instead. So you can choose how bright it is via code.

local onoff = 2

--- onoff = 2 then light is on
--- onoff = 1 then light is off

function onClicked(playerWhoClicked)
if onoff == 1 then
script.Parent.SpotLight.Range = 10
onoff = 2
else
script.Parent.SpotLight.Range = 0
onoff = 1
end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)


If onoff equals 2 at the start, the light is already on prior to the game starting. If onoff equals 1 at the start, the light is off prior to the game starting.

That code also assumes that the Script, ClickDetector and SpotLight is in the same brick.

This code also applies to a PointLight, you just have to change the word SpotLight to PointLight.

0