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

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

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

✓ Accepted answer

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

4 comments

0

FUN!

0

To me, that seems about right. Thanks! I'll try this and if it works, I'll accept your answer.

0

Works fantastically. Thank you very much, I appreciate it.

0

Haha, no prob, glad I could help.

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