How Do I Cycle Using A ClickDetector?

Hello, I want a clickdetector to cycle through a click. For example, click it, it turns green, click it again, its red, click again, green and it cycles per click. Here is my script I tried and did not work.
script.Parent.ClickDetector.MouseClick:Connect(function() script.Parent.Parent.TVScreen.SurfaceGui.VideoFrame.Visible = true script.Parent.BrickColor = BrickColor.new("Bright green") script.Parent.ClickDetector.MouseClick:Connect(function() script.Parent.Parent.TVScreen.SurfaceGui.VideoFrame.Visible = false script.Parent.BrickColor = BrickColor.new("Really red") end) end)

Just make a variable, maybe name it as color, containing a Boolean value. Then use an if statement to check if the Boolean value is true, if it’s true, make it Green, otherwise make it red.

1 Like

Oh, I actually never thought of that. Thanks so much!

TIP: If you wanna make more colors, consider using a table and just make it so that a variable contains an index of the table, then whenever someone click the ClickDetector, change the index of the variable and display whatever color that the variable has indexes on that table. Sounds confusing but I hope you understand since Boolean value only has 2 maximum value.

1 Like
local Cycle = {Color3.fromRGB(255, 0, 0), Color3.fromRGB(0, 255, 0), Color3.fromRGB(0, 0, 255)}
local ClickDetector = script.Parent


local CycleIndex = 1
local function Update()
	ClickDetector.Parent.Color = Cycle[CycleIndex]
end
Update()


ClickDetector.MouseClick:Connect(function()
	if CycleIndex == #Cycle then
		CycleIndex = 1
	elseif CycleIndex < #Cycle then
		CycleIndex += 1
	end
	Update()
end)