Retrieve TextButton that got clicked

I was wondering if you can retrieve a textbutton instance when you click it

I tried this and it doesnt work, maybe there is another way?

local button = script.Parent.TextButton

local function clickButton(buttonThatGotClicked)
      print(buttonThatGotClicked.Name)
end)

button.MouseButton1Click:Connect(clickButton)

Well… you’re connection is parented under the TextButton, I assume, so what is your use case for this?

1 Like

Is this in a local script or server script?

1 Like

I’m trying to make a shop with multiple items, and when it gets clicked I want that the name of the button gets sent to the function.
So that the script can check which item has been clicked on so it can show the player the cost, description and name of said item

It’s local script, inside the a Gui

You can take the script.Parent.Name to send, or simply script.Parent

Or in your case, button.

1 Like

Does an error pop up? If so what error?

1 Like

You can do a for loop and connect the buttons so do something like

For i,v in pairs(buttonholder:getchildren) do
v.mousebutton1down:connect(function()
Print(v.name)
End)
End

2 Likes
Players.g382.PlayerGui.ShopGui.ShopHandler:66: attempt to index nil with 'Name'  -  Client - ShopHandler:66

You can not do this as Gui Buttons do not have arguments to access.

print(buttonThatGotClicked.Name)

At least I think you can’t, I tried and it errored.

1 Like

To add to my original reply if you still want to use that function just make the name of the button a parameter once the connection realizes its clicked.

1 Like

Oh my god, thank you so much, it worked!

Haha no worries and you can use that logic for any connection you need to retrieve properties from what its connected to.

1 Like