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

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

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

✓ Accepted answer

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

2 comments

0

god why is no one using Module Scripts. What if it isn't a children of a frame.

0

I definitely agree with that...module scripts are under rated.

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

1 comment

0

or you can use module script if it isn't in the same location.

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