How can I make this change back to green?

I am making a control panel, and I don’t know how to make it change back to green.
I am bad at scripting if you can’t tell.

Current script.

function onClicked()
script.Parent.BrickColor = BrickColor.new(“Really red”)
end

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

What should I add to make it change back to green on another click?

Make it so there is a debounce like function, and please use code blocks with back ticks.

You can make it so you have a variable that is set to 1 when it is red and 2 when it is green, so therefore if the value is 2, it will change it to 1.

You can use a bool value (true/false) to determine if the button is in the on or off state. and then use an if statement to determine how the button should respond to a click.

local activated = false

local function onClicked()
   if activated == false then
      script.Parent.BrickColor = BrickColor.new("Really red")
   else
      --Other color state goes here
   end
   activated = not activated --True will be false & false will be true. This will be used to flip between the two states.
end

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

Have a simple variable (boolean) and toggle it. If the first time it’s false, set it to green and if it isn’t false, set it to green.

Word of advice: Use local functions ( So that it’s on the stack, (stacks are faster than global environments )) and stay away from deprecated functions.

local debounce = false

local function onClicked()
    if debounce then 
         script.Parent.BrickColor = BrickColor.new("BrightRed")
    else
         script.Parent.BrickColor =  BrickColor.new("Forest Green")
    end
    debounce = not debounce -- not is the opposite
end

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