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

If, True, False statements not working for light system? [ANSWERED]

Asked by 9 years ago

I've made a code that operates from one button, but it doesn't work, can you identify it?

01local light1 = script.Parent.Parent.L1.SpotLight
02local light2 = script.Parent.Parent.L2.SpotLight
03 
04function onClicked(mouse)
05    if light1.Enabled == false and light2.Enabled == false then
06        light1.Enabled = true
07        light2.Enabled = true
08        light1.Transparency = 1
09        light2.Transparency = 1
10    end
11 
12    if light1.Enabled == true and light2.Enabled == true then
13        light1.Enabled = false
14        light2.Enabled = false
15        light1.Transparency = 0
View all 21 lines...

1 answer

Log in to vote
2
Answered by
dyler3 1510 Moderation Voter
9 years ago

You just made 1 simple mistake, which I have learned from making many times myself. You used the wrong event for the ClickDetector. MouseButton1Click is an event for GUI Objects, such as a TextButton, whereas the event you are looking for is MouseClick.


To fix it, we'll simply swap that out with the correct event, and your code should work:

01local light1 = script.Parent.Parent.L1.SpotLight
02local light2 = script.Parent.Parent.L2.SpotLight
03 
04function onClicked(mouse)
05    if light1.Enabled == false and light2.Enabled == false then
06        light1.Enabled = true
07        light2.Enabled = true
08        light1.Transparency = 1
09        light2.Transparency = 1
10 
11    elseif light1.Enabled == true and light2.Enabled == true then --Also I simplified this by switching it to an elseif statement. Feel free to use the code with or without this
12        light1.Enabled = false
13        light2.Enabled = false
14        light1.Transparency = 0
15        light2.Transparency = 0
16 
17    end
18end
19 
20script.Parent.ClickDetector.MouseClick:connect(onClicked)

And now this should work!


If you have any further problems/questions, please leave a comment below and I'll see what I can do. Hope I helped :P

Also, as Cheeky pointed out above, it would be simpler to just use an elseif instead of using two different if statements. It's more convenient, and allows the code to run smoother.
Ad

Answer this question