Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Scriptecx 124
7 years ago

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

Log in to vote
1
Answered by 7 years ago

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
0
Alright, thanks. Having it in a for loop looks ugly to me, but I guess I'll have to keep it like that. Scriptecx 124 — 7y
Ad

Answer this question