How to get pressed button among another

We have some buttons in GUI.
When any button is pressed the function is played but script need to know what button is.
How?

local function Nuh(Button)
	print(Button.Name) --error
end
script.Parent.Slots.Slot1.MouseButton1Click:Connect(Nuh)
script.Parent.Slots.Slot2.MouseButton1Click:Connect(Nuh)
script.Parent.Slots.Slot3.MouseButton1Click:Connect(Nuh)


1 Like

Instead of sending Nuh as a parameter, just send the button object itself

1 Like

script.Parent.Slots.Slot1/2/3.MouseButton1Click:Connect( Function Name, not button name!)
image

Oh sorry, I didn’t read that right. I meant you could send the button object as a parameter to the function, which in your case would be nuh()

I don’t think it’s possible to pass a variable to a function that is connected in this way, Correct me if I am wrong, but I think what needs to happen is this:

local function Nuh(Button)
	print(Button.Name)
end
script.Parent.Slots.Slot1.MouseButton1Click:Connect(function()
	Nuh(script.Parent.Slots.Slot1)
end)
2 Likes

Exactly what I meant, sorry for not specifying

I think it will work but how does Nuh knows what is “Button”? Just create: Button = Slot1/2/3 and its working! Thanks for support

We can make variables and functions easily, we can even pass them around:

local Number = 2

local function Doubler(variable)
	return variable*2
end

local DoubleTheNumber=Doubler(Number)

print(DoubleTheNumber)--Prints 4

We pass the Number Variable to the function, and get its output.

What we are doing in the previous code I sent is attaching the MouseButton1Click to a new function
Then we pass to Nuh the new variable which is the script.Parent.Slots.Slot1, this is the slot1 object. That gets assigned to variable Button and then we simply print the name. You can change which name gets printed easily by simply changing this:

Nuh(script.Parent.Slots.Slot1)

To this:

Nuh(script.Parent.Slots.Slot2)

I recommend this video to learn more about Events and :Connect()

1 Like

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