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
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)