Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a button that changes color once I click it and reverts back when I click it again?

I want to make a button that changes color once I click it and reverts back to normal once I click it again. I made a script, it does not work. Here is the script.

			function leftClick()
	script.Parent.TextColor3 = Color3.new(0, 255, 0)
end
		function leftClick()
	script.Parent.TextColor3 = Color3.FromRGB(255,0,0)
end

script.Parent.MouseButton1Click:Connect(leftClick)

2 answers

✓ Accepted answer
local on = false -- Set it to false if it's off when the game starts (on is true)

local function leftClick()
	if not on then
		script.Parent.TextColor3 = Color3.fromRGB(255, 0, 0)
		on = true
	else
		script.Parent.TextColor3 = Color3.fromRGB(0, 255, 0)
		on = false
	end
end

script.Parent.MouseButton1Click:Connect(leftClick)

3 comments

0

There was a mistake on your script, I fixed it. You forgot to make local on = true, if not not on.

0

oops! yeah, sorry about that

0

I fixed the script

**Is the button a GUI or a part of the workspace? **

*You need to use a click detector. Put one into you object. *

ClickDetector.MouseClick:Connect(function()
	--Write code here to change colors.
end)

2 comments

0

GUI Button, not the surface label.

0

I think he is talking about a GUI button because the event is MouseButton1Click which is for Gui buttons

This discussion is preserved as it was. New answers, comments, and votes are closed.