Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What's the best way to connect a function to a bunch of text buttons?

So I have a bunch of buttons right now, and this is what I have to connect the same function to each of them:

for _, button in pairs (buttons:GetChildren()) do
	if button:IsA("TextButton") then 
		button.MouseEnter:connect(function()
			button.TextSize = 21
		end)
		
		button.MouseLeave:connect(function()
			button.TextSize = 20
		end)
		
		button.MouseButton1Down:connect(function()
			animateTopBarButtons(button)
		end)
	end
end

Is this the best way to do this, or should I connect each button separately? (which will take up a ton of lines)

1 answer

✓ Accepted answer

If you want each of the buttons to act in the same way upon user interaction, then what you have is fine. It would probably be more robust to group the buttons together in some way, perhaps by making them children of the same parent, so you don't have to iterate over every GUI element and run

if button:IsA("TextButton") then
	...
end

1 comment

0

Alright, thanks. Having it in a for loop looks ugly to me, but I guess I'll have to keep it like that.

This discussion is preserved as it was. New answers, comments, and votes are closed.