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

Problems with variables. I can't figure out how to set up this script. Help please?

Asked by 8 years ago

Trying to make a click detector button for different types of lights. All of the lights are billboard gui's, but I am just trying to enable and disable to shut them off. Also the lights are all individual. I feel like I'm missing a variable. Help please?

``

local isOn = false

function on()
 for i = 1, 18 do
        local light = script.Parent["RedLight" .. i].RedLight.Enabled
isOn = true

end

function off()
for i = 1, 18 do
        local light = script.Parent["RedLight" .. i].RedLight.Enabled
isOn = false
end

function onClicked()

 if isOn == true then off() else on() end

end

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

on()

1 answer

Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
8 years ago

I'm not completely sure what you're trying to accomplish.. but from the looks of it, you want to toggle a certain something whenever the ClickDetector is clicked...

If yes, you can simply make use of the if-else statement, which would allow you to execute code if a condition is true, or another piece of code if not true (false/nil).

All you would have to do is set up a function to toggle a pre-set boolean value, and using whatever said value for your works.

local status = false
 -- assigning `status` to `false`

local toggle = function()
    status = not status -- toggling boolean value to its opposite.
    -- false would become true and vice versa

    if status then -- if true then
        print(status) -- true 
        for index = 1, 18 do
            local light = script.Parent["RedLight"..index].RedLight.Enabled
        end
    else -- if not true (false/nil) then 
        print(status) -- false
        for index = 1, 18 do
            local light = script.Parent["RedLight"..index].RedLight.Enabled
        end
    end
end

script.Parent.ClickDetector.MouseClick:connect(toggle)
0
This is image lights, not stats. dluckey20 25 — 8y
0
I never mentioned "stats".. explain yourself better.. there's no such thing as 'Image lights", so say exactly what instance you are working with. ImageLabel 1541 — 8y
Ad

Answer this question