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
8 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
8 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:

Children=script.Parent:GetChildren()

for _,Child in pairs(Children) do --Goes through and assigns the function to all of them.
    if Child:IsA("TextButton") then
        Child.MouseButton1Click:connect(function()
            --Code here for function
        end)
    end
end

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:

Children={} --Make a table of ALL of the Textbuttons here

for _,Child in pairs(Children) do --Pretty much the same from here
    if Child:IsA("TextButton") then
        Child.MouseButton1Click:connect(function()
            --Code here for function
        end)
    end
end

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 — 8y
0
To me, that seems about right. Thanks! I'll try this and if it works, I'll accept your answer. traigla 75 — 8y
0
Works fantastically. Thank you very much, I appreciate it. traigla 75 — 8y
0
Haha, no prob, glad I could help. dyler3 1510 — 8y
Ad

Answer this question