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

Stage Light On/Off Button Not Working Correctly - Don't know what's wrong?

Asked by 5 years ago

I'm making light controls for a stage. This script turns a stage light off and on.

01local ClickDetector = script.Parent.ClickDetector
02local StageLights = script.Parent.Parent.Parent.Parent.StageLights
03 
04function LightSwitch()
05    if StageLights.BackLeftSpotlight.SpotLight.Brightness==0.2 then
06        StageLights.BackLeftSpotlight.SpotLight.Brightness = 1
07        script.Parent.BrickColor= BrickColor.new("Bright yellow")
08    elseif StageLights.BackLeftSpotlight.SpotLight.Brightness==1 then
09        StageLights.BackLeftSpotlight.SpotLight.Brightness = 0.2
10        script.Parent.BrickColor = BrickColor.new("Bronze")
11    end
12end
13 
14ClickDetector.MouseClick:connect(LightSwitch)

When I test the game and click on the button (which turns the light on and off), it will change color, but only on my first click. Any more clicks won't change the color again. Anyone know what I'm doing wrong? Thanks in advance.

0
Is the brightness being changed? Check that with a print statement in the first line of the function IdealistDeveloper 234 — 5y
0
try hooking up a remote event to your click detectors are you doing this on filtering enabled? Si_SenorTN 25 — 5y

1 answer

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

This is not your fault, this is Roblox's fault. Your code is correct, it is just that Roblox has rounding errors. You can see this error in action when you print the Brightness when it is changed to 0.2. It will print something like 0.20000623. To fix this just edit your code to the following:

01local ClickDetector = script.Parent.ClickDetector
02local StageLights = script.Parent.Parent.Parent.Parent.StageLights
03 
04function LightSwitch()
05    if math.floor(StageLights.BackLeftSpotlight.SpotLight.Brightness*10)/10==0.2 then
06        StageLights.BackLeftSpotlight.SpotLight.Brightness = 1
07        script.Parent.BrickColor= BrickColor.new("Bright yellow")
08    elseif StageLights.BackLeftSpotlight.SpotLight.Brightness==1 then
09        StageLights.BackLeftSpotlight.SpotLight.Brightness = 0.2
10        script.Parent.BrickColor = BrickColor.new("Bronze")
11    end
12end
13 
14ClickDetector.MouseClick:connect(LightSwitch)

What this is doing is multiplying the brightness by 10 to make it about 2, rounding it down to make it equal 2, then dividing it by ten to make it equal 0.2.

0
Thank you! It worked -- glad to know it wasn't exactly my scripting error. VeryRaven 85 — 5y
Ad

Answer this question