Change button Selection

Hello!

While developing my game, I wondered how can you do this:

Let’s say we have 10 buttons (image or text, preferably text). In a GUI.

Let’s name them Button1, Button2 to Button10.

So let’s say they have a color of white (black outlines). When we click one, it turns grey. When we click another one, the previous one turns back to white and the new one the grey. You see where I’m going with this.

Extra info (not necessary)

Just to simplify, let’s make it so we have a TextButton, we click it to open our UI (ImageLabel and the 10 TextButtons). Our first one is grey (Button1)

I wondered how I could do this, but I am a bit of a new scripter. So I’m pretty sure what I would code would be bad and there’s a better way of doing it.

If anyone could tell me how, link me any Devhub link, I would appreciate.

Thanks for reading.

What I do in these cases is create a variable inside the script where I store the current “active” button. When I click another button, it first changes back the outline of the “active” button, then it changes the outline of the current button, and finally, it updates the active variable to the current button that was pressed.

1 Like

I’ll try something and share the code.

Hello on an update!

I did not finish coding this yet. It will be quite a challenge. As of right now I am in online school. So I’ll be available at 4:10pm (depending on time zone), where I live it is around 1:50pm

So, basically. The script kinda works, the part that doesn’t work is, whenever I click another, the color doesn’t go back.

Also I set a Default “active” button, which it does not seem to work too.

Here’s the code:

Code
local Map = script.Parent

local current = nil

wait(0) --Just because of a Stack.

local Cam01 = Map.Cam1

local Cam02 = Map.Cam2

local Cam03 = Map.Cam3

local Cam04 = Map.Cam4

local Cam05 = Map.Cam5

local Cam06 = Map.Cam6

local Cam07 = Map.Cam7

local Cam08 = Map.Cam8

Used = false

local function Coloringcurrent(Thing)

if Used == false then

current = Cam01

Used = true

end

current = Thing

print(current)

if current == not Thing then

current.BackgroundColor3 = Color3.fromRGB(46,46,46)

end

current.BackgroundColor3 = Color3.fromRGB(150, 150, 150)

end

Cam01.MouseButton1Click:Connect(function()

Coloringcurrent(Cam01)

current = Cam01

end)

Cam02.MouseButton1Click:Connect(function()

Coloringcurrent(Cam02)

current = Cam02

end)

--This goes on until 08

I probably did a mistake of me doing one thing wrong.

On the OutPut, there are no errors.

Something like this

local Map = script.Parent

local Cams = {}

-- use a for loop to easily add all cams to a table
for i = 1, 8 do
	local Cam = Map:FindFirstChild("Cam" .. i)
	if Cam then
		table.insert(Cams, Cam)
	end
end

local LastCam
-- iterate the table of cams from earlier
for i, Cam in ipairs(Cams) do
	-- when cam is clicked
	Cam.MouseButton1Click:Connect(function()
		-- if a cam was clicked before and the last cam isn't the cam you clicked
		if LastCam and LastCam ~= Cam then
			LastCam.BackgroundColor3 = Color3.fromRGB(46, 46, 46)
		end
		-- set LastCam to cam you clicked
		LastCam = Cam
		Cam.BackgroundColor3 = Color3.fromRGB(150, 150, 150)
	end)
end
2 Likes

Thank you! I’m new to loops like in pairs and ipairs. You made a lot of things much simplier and now how I know a bit more.