How can I detect when someone clicks any GUIButton inside a frame

Hello! I am trying to figure out how to make it so whenever someone clicks a GUIButton inside a specific frame it for example prints.

So I am trying to do something like this

for _, v in pairs(l_teleportContainer_2) do
	v.MouseButton1Click:Connect(function()
		if typeof(v) == "GuiButton" then
			print("Clicked".. v)
		else
			print("Is not a GuiButton")
		end
	end)
end

But you cannot find pairs inside a frame, is there a way I am able to? The reason why I am trying to do this, is because the buttons automatically are added from a script instead of it being added manually, so I am trying to make it so it detects when a button is being clicked, how could I do that?

If you need a better explanation, I will gladly give you! Thanks! :smile:

use:

in pairs(container:GetChildren())

Okay, that seems to work fine.

for _, v in ipairs(l_teleportContainer_2:GetChildren()) do
	if v:IsA("GuiButton") then
		v.MouseButton1Click:Connect(function()
			print("Clicked")
		end)
	else
		print("is not a guibutton")
	end
end

How would I be able to make it when it prints “Clicked” also show the buttons name that it clicked to make sure that it clicked the right one.

for _, v in ipairs(l_teleportContainer_2:GetChildren()) do
	if v:IsA("GuiButton") then
		v.MouseButton1Click:Connect(function()
			print("Clicked") ; print(v.Name)
		end)
	else
		print("is not a guibutton")
	end
end
1 Like

Thank you! It worked, I appreciate it!