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

How can I make a function for multiple gui buttons?

Asked by 7 years ago

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

Log in to vote
3
Answered by 7 years ago

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

Ad

Answer this question