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

Is there an easy way to script 48 different text buttons into one call function in a script?

Asked by
traigla 75
9 years ago

I know I'm supposed to provide a code, but I'm unsure on this one. I have 48 text buttons all of which I want to do similar code. Whenever I click any of them, I want them to run the same function. Is there an easy way to do this without having to write out 48 different calls?

Thanks.

1 answer

Log in to vote
3
Answered by
dyler3 1510 Moderation Voter
9 years ago

There actually is a pretty simple way of doing this with a loop. If they all have the same parent, then you could easily just do it this way:

1Children=script.Parent:GetChildren()
2 
3for _,Child in pairs(Children) do --Goes through and assigns the function to all of them.
4    if Child:IsA("TextButton") then
5        Child.MouseButton1Click:connect(function()
6            --Code here for function
7        end)
8    end
9end

Anyways, this would be the simplest way of doing it. Another way however if they don't have the same parent would be to do this:

1Children={} --Make a table of ALL of the Textbuttons here
2 
3for _,Child in pairs(Children) do --Pretty much the same from here
4    if Child:IsA("TextButton") then
5        Child.MouseButton1Click:connect(function()
6            --Code here for function
7        end)
8    end
9end

Anyways, I hope this helped you out a bit. If you have any further problems/questions, please leave a comment below, and I'll see what I can do :P

0
FUN! viralmoose 65 — 9y
0
To me, that seems about right. Thanks! I'll try this and if it works, I'll accept your answer. traigla 75 — 9y
0
Works fantastically. Thank you very much, I appreciate it. traigla 75 — 9y
0
Haha, no prob, glad I could help. dyler3 1510 — 9y
Ad

Answer this question