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

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

Asked by 5 years ago

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

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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)
0
Damit, I am typing on a digital device, you wrote faster:( Ziffixture 6913 — 5y
0
B) Gey4Jesus69 2705 — 5y
0
You have enough rep, can you leave some?;) Ziffixture 6913 — 5y
0
i just want a hEcKiNg uPvOtE. i need 20 for next rank Gey4Jesus69 2705 — 5y
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? Ziffixture 6913 — 5y
Ad

Answer this question