Trying to select / deselect gui buttons

hello! im trying to make my buttons so that only one can go green if one is selected and not more than that. how would you do that? i have the buttons cloned for each client so instead of having it in a folder a button is in each client? ty!

dd40a6009744e7588951e76c3131e6ca

main script:


local template = game.ReplicatedStorage.plrName
local plrs = game:GetService("Players")

function updPlrs()

	local plr = game.Players.LocalPlayer
	local thumbnailtype = Enum.ThumbnailType.HeadShot
	local thumbnailsize = Enum.ThumbnailSize.Size420x420

	for i, p in pairs(game.Players:GetPlayers()) do

		if script.Parent.plrScroll:FindFirstChild(p.Name) then continue end

		local thumbnail = plrs:GetUserThumbnailAsync(p.UserId, thumbnailtype, thumbnailsize)

		local templateClone = template:Clone()

		templateClone.Name = p.Name
		templateClone.Parent = script.Parent.plrScroll
		templateClone.Visible = true
		templateClone.Text = p.Name
		templateClone.displayName.Text = "@"..p.DisplayName
		templateClone.PFP.Image = thumbnail

	end
end

function removeSlot(plr)
	for i, child in pairs(script.Parent.plrScroll:GetChildren()) do
		if child.Name == plr.Name then
			child:Destroy()
		end
	end
end

updPlrs()
game.Players.PlayerAdded:Connect(updPlrs)
game.Players.PlayerRemoving:Connect(removeSlot)

script:

local tbox = script.Parent
local displayName = script.Parent.displayName
local db = false

tbox.MouseButton1Click:Connect(function()
	if db == false then
		tbox.BackgroundColor3 = Color3.fromRGB(97, 255, 62)
		print(tbox.Text)
		db = true
	elseif db == true then
		tbox.BackgroundColor3 = Color3.fromRGB(199, 199, 199)
		print(tbox.Text)
		db = false
	end
end)

You could get all the buttons and then turn their color to white, and then apply the green color to the button you have selected. Hope I am understanding your question correctly.

local tbox = script.Parent
local displayName = script.Parent.displayName
local db = false

tbox.MouseButton1Click:Connect(function()
	if db == false then
		for i,v in pairs(tbox.Parent:GetChildren()) do
			if v:IsA("TextButton") then
				v.BackgroundColor3 = Color3.fromRGB(199, 199, 199)
			end
		end
		tbox.BackgroundColor3 = Color3.fromRGB(97, 255, 62)
		print(tbox.Text)
		db = true
	elseif db == true then
		tbox.BackgroundColor3 = Color3.fromRGB(199, 199, 199)
		print(tbox.Text)
		db = false
	end
end)

You should handle the MouseButton1Click event in one central LocalScript instead of having a LocalScript in each button and have a variable store the selected button. To do this, you can move the MouseButton1Click event to the for loop in your updPlrs() function.

local unselectedColor = Color3.fromRGB(250, 250, 250) -- grey/white
local selectedColor = Color3.fromRGB(0, 250, 0) -- Green

local selectedButton -- Stores the button you clicked on

function updPlrs()
	local plr = game.Players.LocalPlayer
	local thumbnailtype = Enum.ThumbnailType.HeadShot
	local thumbnailsize = Enum.ThumbnailSize.Size420x420

	for i, p in pairs(game.Players:GetPlayers()) do=
		if script.Parent.plrScroll:FindFirstChild(p.Name) then continue end
		local thumbnail = plrs:GetUserThumbnailAsync(p.UserId, thumbnailtype, thumbnailsize)

		local templateClone = template:Clone()=
		templateClone.Name = p.Name
		templateClone.Visible = true
		templateClone.Text = p.Name
		templateClone.displayName.Text = "@"..p.DisplayName
		templateClone.PFP.Image = thumbnail
		templateClone.Parent = script.Parent.plrScroll
                
		templateClone.MouseButton1Click:Connect(function()
			if selectedButton == templateClone then return end
			if selectedButton then
				selectedButton.BackgroundColor3 = unselectedColor -- Reset the current selected button's color
			end

			templateClone.BackgroundColor3 = selected and selectedColor or unselectedColor
			selectedButton = templateClone -- Rewrite the selectedButton

			print(tbox.Text)
		end)
	end
end

hey oopsi, thanks for ur response. it nearly works but it takes like two clicks to enable/disable it pretty much but tysm! basically if a player selects another button then the previous button they clicked on will be grey and the newly clicked button will be green. hopefully that makes sense. :slight_smile:

im trying to integrate it in this script

local unselectedColor = Color3.fromRGB(199, 199, 199) -- grey/white
local selectedColor = Color3.fromRGB(97, 255, 62) -- Green

local selectedButton -- Stores the button you clicked on

function updPlrs()
	local plr = game.Players.LocalPlayer
	local thumbnailtype = Enum.ThumbnailType.HeadShot
	local thumbnailsize = Enum.ThumbnailSize.Size420x420

	for i, p in pairs(game.Players:GetPlayers()) do
		if (script.Parent.plrScroll:FindFirstChild(p.Name)) then continue end
		local thumbnail = plrs:GetUserThumbnailAsync(p.UserId, thumbnailtype, thumbnailsize)

		local templateClone = template:Clone()
		templateClone.Name = p.Name
		templateClone.Visible = true
		templateClone.Text = p.Name
		templateClone.displayName.Text = "@"..p.DisplayName
		templateClone.PFP.Image = thumbnail
		templateClone.Parent = script.Parent.plrScroll

		templateClone.MouseButton1Click:Connect(function()
			if selectedButton == templateClone then return end
			if selectedButton then
			selectedButton.BackgroundColor3 = unselectedColor -- Reset the current selected button's color
		end

			templateClone.BackgroundColor3 = selectedButton and selectedColor or unselectedColor
			selectedButton = templateClone -- Rewrite the selectedButton

			print(templateClone.Text)
		end)
end
end

function removeSlot(plr)
	for i, child in pairs(script.Parent.plrScroll:GetChildren()) do
		if child.Name == plr.Name then
			child:Destroy()
		end
	end
end

updPlrs()
game.Players.PlayerAdded:Connect(updPlrs)
game.Players.PlayerRemoving:Connect(removeSlot)

hey man, thanks a million for ur response. it pretty much works perfectly but the only problem i have is that you have to click say one slot than another to get it to work? sorry its hard to explain i made a video. ty for ur help once again! :smiley:

EDIT: i fixed it:

local unselectedColor = Color3.fromRGB(199, 199, 199) -- grey/white
local selectedColor = Color3.fromRGB(97, 255, 62) -- Green

local selectedButton -- Stores the button you clicked on

function updPlrs()
	local plr = game.Players.LocalPlayer
	local thumbnailtype = Enum.ThumbnailType.HeadShot
	local thumbnailsize = Enum.ThumbnailSize.Size420x420

	for i, p in pairs(game.Players:GetPlayers()) do
		if (script.Parent.plrScroll:FindFirstChild(p.Name)) then continue end
		local thumbnail = plrs:GetUserThumbnailAsync(p.UserId, thumbnailtype, thumbnailsize)

		local templateClone = template:Clone()
		templateClone.Name = p.Name
		templateClone.Visible = true
		templateClone.Text = p.Name
		templateClone.displayName.Text = "@"..p.DisplayName
		templateClone.PFP.Image = thumbnail
		templateClone.Parent = script.Parent.plrScroll

		templateClone.MouseButton1Click:Connect(function()
			if selectedButton == templateClone then return end
			if selectedButton then
			selectedButton.BackgroundColor3 = unselectedColor -- Reset the current selected button's color
		end

			templateClone.BackgroundColor3 = templateClone and selectedColor or unselectedColor
			selectedButton = templateClone -- Rewrite the selectedButton

			print(templateClone.Text)
		end)
end
end

function removeSlot(plr)
	for i, child in pairs(script.Parent.plrScroll:GetChildren()) do
		if child.Name == plr.Name then
			child:Destroy()
		end
	end
end

updPlrs()
game.Players.PlayerAdded:Connect(updPlrs)
game.Players.PlayerRemoving:Connect(removeSlot)