Having issues with color changing back

Basically, what I’m trying to make is when you hover over the button, it changes to red and stays like that until you hover on another button. Now the changing it to red part works, but none of them change back when I hover over something else. Here’s my code, and a video to show the issue I’m having.

local buttons = {
	script.Parent.MenuBack.Buttons.Continue,
	script.Parent.MenuBack.Buttons.New,
	script.Parent.MenuBack.Buttons.Quit,
	script.Parent.MenuBack.Buttons.Settings
}

local regularColor = Color3.new(1,1,1)
local hoverTextColor = Color3.new(0.701961, 0, 0)
local currentSelectedButton = buttons[1].Parent.buttonSelected



buttons[1].MouseEnter:Connect(function()
	buttons[1].TextColor3 = hoverTextColor
	buttons[1].TextLabel.TextColor3 = hoverTextColor
	if currentSelectedButton.Value == 2 then
		currentSelectedButton.Value = 1
		buttons[2].TextColor3 = regularColor
		buttons[2].TextLabel.TextColor3 = regularColor
	elseif currentSelectedButton.Value == 3 then
		currentSelectedButton.Value = 1
		buttons[3].TextColor3 = regularColor
		buttons[3].TextLabel.TextColor3 = regularColor
	elseif currentSelectedButton.Value == 4 then
		currentSelectedButton.Value = 1
		buttons[4].TextColor3 = regularColor
		buttons[4].TextLabel.TextColor3 = regularColor
	end
end)

buttons[2].MouseEnter:Connect(function()
	buttons[2].TextColor3 = hoverTextColor
	buttons[2].TextLabel.TextColor3 = hoverTextColor
	if currentSelectedButton.Value == 1 then
		currentSelectedButton.Value = 2
		buttons[1].TextColor3 = regularColor
		buttons[1].TextLabel.TextColor3 = regularColor
	elseif currentSelectedButton.Value == 3 then
		currentSelectedButton.Value = 2
		buttons[3].TextColor3 = regularColor
		buttons[3].TextLabel.TextColor3 = regularColor
	elseif currentSelectedButton.Value == 4 then
		currentSelectedButton.Value = 2
		buttons[4].TextColor3 = regularColor
		buttons[4].TextLabel.TextColor3 = regularColor
	end
end)

buttons[3].MouseEnter:Connect(function()
	buttons[3].TextColor3 = hoverTextColor
	buttons[3].TextLabel.TextColor3 = hoverTextColor
	if currentSelectedButton.Value == 1 then
		currentSelectedButton.Value = 3
		buttons[1].TextColor3 = regularColor
		buttons[1].TextLabel.TextColor3 = regularColor
	elseif currentSelectedButton.Value == 2 then
		currentSelectedButton.Value = 3
		buttons[2].TextColor3 = regularColor
		buttons[2].TextLabel.TextColor3 = regularColor
	elseif currentSelectedButton.Value == 4 then
		currentSelectedButton.Value = 3
		buttons[4].TextColor3 = regularColor
		buttons[4].TextLabel.TextColor3 = regularColor
	end
end)

buttons[4].MouseEnter:Connect(function()
	buttons[4].TextColor3 = hoverTextColor
	buttons[4].TextLabel.TextColor3 = hoverTextColor
	if currentSelectedButton.Value == 1 then
		currentSelectedButton.Value = 4
		buttons[1].TextColor3 = regularColor
		buttons[1].TextLabel.TextColor3 = regularColor
	elseif currentSelectedButton.Value == 2 then
		currentSelectedButton.Value = 4
		buttons[2].TextColor3 = regularColor
		buttons[2].TextLabel.TextColor3 = regularColor
	elseif currentSelectedButton.Value == 3 then
		currentSelectedButton.Value = 4
		buttons[3].TextColor3 = regularColor
		buttons[3].TextLabel.TextColor3 = regularColor
	end
end)


And sorry for making you watch that whole scene, skip about like 20 or so seconds in.
Also if you have any recommendations on how to make this code much shorter please feel free to let you know. I’m probably doing this the worst way possible.

You could try using the MouseLeave function and just change the color back to its original color. That would be a lot simpler.

I mean thats not what I’m trying to do really. I want the color to stay the same even after you stop hovering off of it, until you hover over something else. Just got an idea for something, give me a second to implement it, in the meanwhile send over any other ideas you have.

You could define all of your text labels as variables and use the mouse enter function for each one and add a script to change all colors and sizes, ect. I would be less code and you could see where you made a mistake. It would be less code and easier to understand, I really hope that helps :smile:

My way actually worked way better, and its actually tons shorter! Here’s what I ended up doing:

buttons[1].MouseEnter:Connect(function()
	for i, v in ipairs(buttons) do
		v.TextColor3 = regularColor
		v.TextLabel.TextColor3 = regularColor
		buttons[1].TextColor3 = hoverTextColor
		buttons[1].TextLabel.TextColor3 = hoverTextColor
	end
end)

buttons[2].MouseEnter:Connect(function()
	for i, v in ipairs(buttons) do
		v.TextColor3 = regularColor
		v.TextLabel.TextColor3 = regularColor
		buttons[2].TextColor3 = hoverTextColor
		buttons[2].TextLabel.TextColor3 = hoverTextColor
	end
end)

buttons[3].MouseEnter:Connect(function()
	for i, v in ipairs(buttons) do
		v.TextColor3 = regularColor
		v.TextLabel.TextColor3 = regularColor
		buttons[3].TextColor3 = hoverTextColor
		buttons[3].TextLabel.TextColor3 = hoverTextColor
	end
end)

buttons[4].MouseEnter:Connect(function()
	for i, v in ipairs(buttons) do
		v.TextColor3 = regularColor
		v.TextLabel.TextColor3 = regularColor
		buttons[4].TextColor3 = hoverTextColor
		buttons[4].TextLabel.TextColor3 = hoverTextColor
	end
end)

Thanks for the help though anyways, made me think about different ways I could have done it.

1 Like

I’m glad I could help, :relaxed: keep up the good work
:+1: Also good job that had me stumped

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.