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

How can I use ClickDetector to turn a light on and off?

All right, so I before I explain my issues I'd like to say I've only been learning Roblox lua for the good part of two days. I have surfed the entirety of the internet trying to find a solution to this and rewritten the code at least a hundred times, and I have yet to figure out how to rig a simple on and off switch for a light. At first, I noticed light could only be turned off and was not able to be turned back on. I then tried to put it into a while loop, which showed in the output that it was turning the light on very quickly and then off again. In conclusion, I am unsure of how to organize the code so that the script reads the code in a way that it will understand that when the light is Off, it will turn it On. I'm not asking for anyone to write the script for me, all I need is a shove in the right direction. Thanks!

local Light = script.Parent.PointLight

local Click = Instance.new("ClickDetector")
Click.Parent = script.Parent
Click.MaxActivationDistance = 10

if Light.Enabled == false then Click.MouseClick:Connect(function()
	Light.Enabled = true
	print("Light turned on")
end)
elseif Light.Enabled == true then Click.MouseClick:Connect(function()
	Light.Enabled == false
	print("Light turned off")
end)
end

1 answer

✓ Accepted answer

just fix your syntax for your event

local Light = script.Parent.PointLight

local Click = Instance.new("ClickDetector")
Click.Parent = script.Parent
Click.MaxActivationDistance = 10


Click.MouseClick:Connect(function(plr)
  if Light.Enabled == false then
    Light.Enabled = true
    print("Light turned on")
  else
    Light.Enabled = false
    print("Light turned off")
  end
end)

I used the if/else statement just to show you the prints. Note you could also do:

Click.MouseClick:Connect(function(plr)
  Light.Enabled = not Light.Enabled --sets it to opposite value
end)

5 comments

0

Damit, I am typing on a digital device, you wrote faster:(

0

B)

0

You have enough rep, can you leave some?;)

0

i just want a hEcKiNg uPvOtE. i need 20 for next rank

0

woah, buddy, slow down here, you've helped me though so I'll do you a solid, but I expect a solid in return?

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