How to obtain the original TextButton of a `MouseButton1Down` event?

I have a hundred MouseButton1Down events and I need to obtain the original TextButton object that triggered the event, inside each function.
Currently, I have to manually declare the object name one by one inside the print:

TextButton1.MouseButton1Down:Connect(function() print(TextButton1) end)
TextButton2.MouseButton1Down:Connect(function() print(TextButton2) end)
TextButton3.MouseButton1Down:Connect(function() print(TextButton3) end)

I wonder if there is any automatic way to get this GUI object that fired the event, something like self (although self doesn’t work in this case):

TextButton1.MouseButton1Down:Connect(function() print(self) end) -- should print 'TextButton1'
TextButton2.MouseButton1Down:Connect(function() print(self) end) -- should print 'TextButton2'
TextButton3.MouseButton1Down:Connect(function() print(self) end) -- should print 'TextButton3'

I don’t think there is a different method than the one you are currently using. I have at least never seen any alternatives.

But this usually isn’t an issue, as this type of stuff usually is done in a loop, which means it turns into:

for _, btn in pairs(buttons) do
	btn.Activated:Connect(function()
		print(btn)
	end)
end
1 Like

I think the only thing you can do here is create the buttons and store the references in a table and connect each one with the same method as you are already doing, and store this connection in the same table or as a separate connection table with the buttons as an index to reference them. When connecting the buttons to the event you are connecting to just implicitly define your function inline within the code creating the buttons.