The code below is just trying to turn on the SpotLight placed in part but the issue I have is after activating the light and clicking the part once again to turn it off, the script refuses to run the function and does not return any errors in output.
light = script.Parent.SpotLight charge = script.Parent.Charge.Value button = script.Parent.ClickDetector function enable() if charge >= 100 then light.Enabled = true charge = (charge - 20) print ("on") print (charge) elseif light.Enabled == true then print ("Already on, turning off") wait (5) light.Enabled = false end end script.Parent.ClickDetector.MouseClick:connect(enable)
What happens in your script is that the code will go from top to bottom. It will encounter the first if case: Charge >= 100
. if this is true, it will not look to any other cases and go about executing the code in the case. so to fix your code, you will have to make the case like this:
if charge >= 100 and not light.Enabled == true then
.
this way it will check if the value of your charge variable, if this is true (e.g. exceeds or is 100) it will also look if the lamp is not already turned on, if this is not the case it will continue with the code in the case, otherwise it will go to the next case which is elseif and test the requirements for that.
hope it helps!
Updated and FIXED code
light = script.Parent.SpotLight charge = script.Parent.Charge.Value button = script.Parent.ClickDetector local isOn = false function enable() if isOn == true then isOn = false print("Already on, turning off") print(charge) wait(0.5) light.Enabled = false elseif charge == 0 then isOn = false print("No charge") light.Enabled = false elseif charge >= 20 then isOn = true charge = (charge - 20) print("Turning on") wait(0.01) light.Enabled = true print(charge) end end script.Parent.ClickDetector.MouseClick:connect(enable)