Best way to call which button out of a list of buttons?

I want to be able to call which button was pressed (which would be v in the code) under a list of buttons, all of which are a child to a single frame.

Right now, it only detects when one of the buttons in my list is activated. I’d like to know when any of the buttons under the frame are activated

I’ve tried looking around for solutions and couldn’t find one that would work for this specifically.
Thank you.

local RS = game:GetService("ReplicatedStorage")
local CharRemote = RS:WaitForChild("CharSwapEvent")
local CharFunc = RS:WaitForChild("CharSwapFunc")
local ButtonFrame = game.StarterGui.CharacterSelectGUI.ButtonFrame
local CharacterFrame = ButtonFrame.CharacterFrame
local Characters = RS.Characters

for i, v in ipairs(script.Parent:GetChildren()) do
	if v:IsA("ImageButton") then
		v.Activated:Connect(function()
			for _, character in Characters:GetChildren() do
				if v.Name ~= character.Name then return end
				CharFunc:InvokeServer(character)
			end
		end)
	end
end

wouldn’t you just loop over all of the buttons in the frame and check to see if each of them were clicked

Just print the name of the button when it is clicked

function Notify(button)
   print('Button '..button..' was activated')
end
local RS = game:GetService("ReplicatedStorage")
local CharRemote = RS:WaitForChild("CharSwapEvent")
local CharFunc = RS:WaitForChild("CharSwapFunc")
local ButtonFrame = game.StarterGui.CharacterSelectGUI.ButtonFrame
local CharacterFrame = ButtonFrame.CharacterFrame
local Characters = RS.Characters

for i, v in ipairs(script.Parent:GetChildren()) do
	if v:IsA("ImageButton") then
		v.Activated:Connect(function()
            Notify(v.Name)
			for _, character in Characters:GetChildren() do
				if v.Name ~= character.Name then return end
				CharFunc:InvokeServer(character)
			end
		end)
	end
end
3 Likes

could you please elaborate a bit on what the problem is?

@MysteryX2076’s code helped me figure out that the code to find which button press was working. The issue is that when the loop to find which character name matches the button name, only one of the character button events work. Whichever button works seems to be random. I can’t explain it any better, so here’s a video of what i mean. Sometimes the print that runs is only for Default.

Here’s also the code that the server runs when the remote fuction is fired.

remoteFunc.OnServerInvoke = function(player, character)
	player:SetAttribute("SetCharacter", character.Name)
	print(player:GetAttribute("SetCharacter"))
end

1 Like

I can draw only one conclusion from your video, the ‘Default’ character doesn’t even exist in the RS.Characters, go check whether it exists and if it is check the spelling

1 Like

It does exist. Sorry, I should’ve elaborated more.
Sometimes it also prints out “Default” normally. It seems to be random which one works. I’m not sure why it does this.

There are two problems with your code.

Firstly, you are not supposed to use return if you want to iterate all characters using a loop in this case. Use continue instead if you don’t want the loop to run any code after the statement.

Secondly, when the client (or server) invoke a RemoteFunction to the server (or client), the client (or server) will yield the current code until the server (or client) returns something. Because the server does not return anything, the client will stay in this yielded state and cannot run code. And because it cannot run any further, chances are that it can only print one button.

Try replacing the RemoteFunction with a RemoteEvent, or add a return at the end of the callback to see if it makes any difference.

1 Like

This worked exactly as I had intended. Thank you guys for the help, I really appreciate.
switching to then continute end and adding a return right after invoking the remotefunction on the client. Thank you :pray: , I’ll keep it in mind for the future.

1 Like

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