How would I make something change when a button is clicked again

I’m currently making a Lockdown system and I want it everything to go back to normal when the button is pressed again and I’m unsure on how to do this.

local LockdownButton = script.Parent
local clickDetector = LockdownButton.ClickDetector
local lights = game.Workspace.Lights

function onMouseClick()

LockdownButton.BrickColor = BrickColor.Red()
for k, light in pairs(lights:getChildren()) do
wait()
light.Light.Color= Color3.fromRGB(255, 58, 58)

end
end

clickDetector.MouseClick:connect(onMouseClick)

I would add a variable named lockdownOn and set it to false then once the lockdown happens set it to true and when you call the function check if it’s true and if so then turn off the lockdown

local LockdownButton = script.Parent
local clickDetector = LockdownButton.ClickDetector
local lights = game.Workspace.Lights
local lockdownOn = false

function onMouseClick()

if lockdownOn == true then
       —turn off lights
           lockdownOn = false
    else

LockdownButton.BrickColor = BrickColor.Red()
lockdownOn = true 
for k, light in pairs(lights:getChildren()) do
wait()
light.Light.Color= Color3.fromRGB(255, 58, 58)

end
end
end

clickDetector.MouseClick:connect(onMouseClick)

Sorry I’m on mobile so the script is a little wacky looking

1 Like

I would go about creating a value to check

local clickDetector = LockdownButton.ClickDetector

local clicked = false

local lights = game.Workspace.Lights

function onMouseClick()
	if not clicked then --Check if it was clicked
		clicked = true --Set it to true
		LockdownButton.BrickColor = BrickColor.Red()
		for k, light in pairs(lights:getChildren()) do
			wait()
			light.Light.Color= Color3.fromRGB(255, 58, 58)
		end
	else --If not clicked then
		clicked = false --Set it to false
		LockdownButton.BrickColor = BrickColor.Green()()
		for k, light in pairs(lights:getChildren()) do
			wait()
			light.Light.Color= Color3.fromRGB(255, 0, 0)
		end
		
	end
end)

	clickDetector.MouseClick:connect(onMouseClick)

Like this up above. Let me know if it worked :slight_smile:

2 Likes

Hmm…

image
Like this?

No replace your old script with the one i pasted up above. The variable is already in the script. Please mark my answer as the solution if it helped you :slight_smile:

Oh, didn’t realize since the lights never changed color from red to white, so I fixed it and it works!

1 Like