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

Efficient way to check for a button click when you have too many GUI buttons?

Asked by 5 years ago

It's tiring to make a function for each one to check if it was clicked. Is there another way to go about doing this? Maybe something like a loop.

2 answers

Log in to vote
0
Answered by 5 years ago

Yes, you would go with a for loop.

LocalScript

for _,v in pairs(frame:GetChildren()) do
    if v:IsA("ImageButton") then
        v.MouseButton1Click:Connect(function()
            -- Fire server or do other code
        end)
    end
end
0
god why is no one using Module Scripts. What if it isn't a children of a frame. Zafirua 1348 — 5y
0
I definitely agree with that...module scripts are under rated. andrewcooke582 -52 — 5y
Ad
Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
5 years ago
Edited 5 years ago
for i, v in pairs(script.Parent:GetChildren()) do --assuming it's in the startergui, and it's a localscript of course
    if v.ClassName:lower():match("button") then --might be wrong, haven't done lua in a while
        v.MouseButton1Click:connect(function(egg)
            --code
        end)
    end
end

Tell me if something errored. I haven't used lua in a while

0
or you can use module script if it isn't in the same location. Zafirua 1348 — 5y

Answer this question