How to send which button has been clicked

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I would like to be able to send to the function which button has been clicked

  1. What is the issue? Include screenshots / videos if possible!

I have no idea how to send which one of the following buttons has been clicked. I would like to send this information to the function.

local function ButtonClicked()
	--code here
end

--Function Start
Buttons[1].MouseButton1Click:Connect(ButtonClicked)
Buttons[2].MouseButton1Click:Connect(ButtonClicked)
Buttons[3].MouseButton1Click:Connect(ButtonClicked)
Buttons[4].MouseButton1Click:Connect(ButtonClicked)
Buttons[5].MouseButton1Click:Connect(ButtonClicked)
Buttons[6].MouseButton1Click:Connect(ButtonClicked)
Buttons[7].MouseButton1Click:Connect(ButtonClicked)
Buttons[8].MouseButton1Click:Connect(ButtonClicked)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried searching around and found nothing

local function ButtonClicked(buttonNumber)
	print(buttonNumber) --now we know
end

for index, button in pairs(Buttons) do --let's loop through the table so it's a lot simpler
	button.MouseButton1Click:Connect(function()
		ButtonClicked(index)  --pass the button number as an argument
	end)
end
3 Likes

Love the “–now we know”" xD
Thank you a lot btw, it works!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.