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

How can I make a function for multiple gui buttons?

I have this...

for _,buttons in pairs(script.Parent.Inventory:GetChildren()) do 
buttons.MouseButton1Click:connect(function()
print("clicked")
end)
end 

My inventory frame has all the slots and everything but there is also a 'UIGridLayout' in it too, so everytime i try running this, it comes up with this error:

08:46:12.334 - MouseButton1Click is not a valid member of UIGridLayout.

Is there a way I can ignore the UiGridLayout and how? I really do appreciate your help!

1 answer

Try checking if the object is a button, and then put in your MouseButton1Click function like so:

for _,buttons in pairs(script.Parent.Inventory:GetChildren()) do
	if buttons:IsA("TextButton") then --if the button is a text button, if its an image button put in "ImageButton" 
		buttons.MouseButton1Click:connect(function()
		print("clicked")
		end)
	end
end

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