Listening to Activated event for multiple buttons at once

My goal is to listen for Activated event inside an array of buttons. That way, Im able to insert as many buttons as I want and by using only one script listen if they are clicked. For now, script below checks for TextButtons in every TextLabel inside StatusGUI, and adds them to Buttons table

My Code so far - LocalScript (named InvestPoints)

local StatsGUI = script.Parent

local Buttons = {}

for i, v in StatsGUI:GetChildren() do
	if v:IsA('TextLabel') then
		local vChild = v:GetChildren()
		for btn, _ in vChild do
			if vChild[btn] ~= nil and vChild[btn]:IsA('TextButton') then
				local vButton = vChild[btn]
				vButton:SetAttribute('Upgrade', v.Name)
				Buttons[vButton.Name] = vButton:GetAttribute('Upgrade')
				print(Buttons)
			end
		end
	end
end

Saw some methods on DevForum but most of them use if statements. I want it to find names by itself (I hope my idea is clear enough)

How it looks in Explorer
image

Any ideas?

local StatsGUI = script.Parent

local Buttons = {}

for i, v in StatsGUI:GetChildren() do
	if v:IsA('TextLabel') then
		local vChild = v:GetChildren()
		for btn, _ in vChild do
			if vChild[btn] ~= nil and vChild[btn]:IsA('TextButton') then
				local vButton = vChild[btn]
				vButton:SetAttribute('Upgrade', v.Name)
				Buttons[vButton.Name] = vButton:GetAttribute('Upgrade')
                vButton.Activated:Connect(functionHere)
				print(Buttons)
			end
		end
	end
end

I believe this is what you’re trying to achieve here?

1 Like

I didn’t know its possible to do this like that! Thank you! It works as I wanted!

1 Like

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