How can 1 script in gui detects multiple button click event?

im trying to avoid adding a script on every button to keep it simple and not that laggy, is there a way that can detects which button is cicked based on the button name?

ive tried this so far, i found it in the devforum but it sadly didn’t work

its a local script

local re = game.ReplicatedStorage:WaitForChild("CharacterRE")

script.Parent.DescendantAdded:Connect(function(Descendant)
	if Descendant:IsA("ViewportFrame") then
		if Descendant:FindFirstChild("Button") then
			Descendant.Button.MouseButton1Click:Connect(function()
				re:FireServer(Descendant.Name)
			end)
		end
	end
end)

the button is child to a viewportframe, that makes the viewport frame able to be clicked

I want it to fire the name of the viewport frame when the button inside it is clicked

Just straight up look for a button

local re = game.ReplicatedStorage:WaitForChild("CharacterRE")

script.Parent.DescendantAdded:Connect(function(Descendant)
	if Descendant:IsA("TextButton") or Descendant:IsA("ImageButton") then
		Descendant.MouseButton1Click:Connect(function()
			re:FireServer(Descendant.Name)
		end)
	end
end)

Make sure to make the Zindex of the button bigger than the viewportframe’s Zindex. Like: Viewportframe Zindex: 1 and Button Zindex: 2

Then make all Button transparencys to 1 so the button is invisible

local re = game.ReplicatedStorage:WaitForChild("CharacterRE")

for Index,Descendant in pairs(script.Parent:GetDescendants()) do
	if Descendant:IsA("TextButton") or Descendant:IsA("ImageButton") then
		Descendant.MouseButton1Click:Connect(function()
			re:FireServer(Descendant.Name)
		end)
	end
end
2 Likes

I changed the
re:FireServer(Descendant.Name)
to
re:FireServer(Descendant.Parent.Name)
since I want it to fire the name of the viewport frame, but it works fine