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 8 years ago

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

local light1 = script.Parent.Parent.L1.SpotLight
local light2 = script.Parent.Parent.L2.SpotLight

function onClicked(mouse)
    if light1.Enabled == false and light2.Enabled == false then
        light1.Enabled = true
        light2.Enabled = true
        light1.Transparency = 1
        light2.Transparency = 1
    end

    if light1.Enabled == true and light2.Enabled == true then
        light1.Enabled = false
        light2.Enabled = false
        light1.Transparency = 0
        light2.Transparency = 0

    end
end

script.Parent.ClickDetector.MouseButton1Click:connect(onClicked)

1 answer

Log in to vote
2
Answered by
dyler3 1510 Moderation Voter
8 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:

local light1 = script.Parent.Parent.L1.SpotLight
local light2 = script.Parent.Parent.L2.SpotLight

function onClicked(mouse)
    if light1.Enabled == false and light2.Enabled == false then
        light1.Enabled = true
        light2.Enabled = true
        light1.Transparency = 1
        light2.Transparency = 1

    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
        light1.Enabled = false
        light2.Enabled = false
        light1.Transparency = 0
        light2.Transparency = 0

    end
end

script.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