Send Name to function

Lets say, im doing this:

function clickedButton()
-- Code
end)

Button.MouseButton1Click:Connect(clickedButton()

How could i do something like this:

function clickedButton(buttonName)
if buttonName == Button1 then
-- Code
end
end)

Button.MouseButton1Click:Connect(clickedButton(Button1)

I also want to send the Button Name, but
Button.MouseButton1Click:Connect(clickedButton(Button1) will not work, it gives me a Error. How could i do it?

1 Like

There is only one way:

Button.MouseButton1Click:Connect(function()
    clickedButton(Button1)
end)

Sadly, no other way.

Okay thanks but i wanted to use

connection = Button.MouseButton1Click:Connect(clickedButton), how could i use it then

local connection

connection = Button.MouseButton1Click:Connect(function()
    clickedButton(Button)
end)

But

connection = Button.MouseButton1Click:Connect(function()
    clickedButton(Button)
end)

will start this Function:

function clickedButton(Button)

will Disconnect() disconnect the clickedButton Function or MouseButton1Click function?

function clickedButton()
if Button.Name == "Button1" then
-- Code
end
end)

Button.MouseButton1Click:Connect(clickedButton)

Can you elaborate on what you want to do?
And how many buttons are you planning to connect?

And Edit: @ayoub50’s example won’t work like you want it to.

I have many Buttons like:

local connection

function buttonClicked()

--Code

connection:Disconnect()

end)

connection = Button1.MouseButton1Click:Connect(buttonClicked)

connection = Button2.MouseButton1Click:Connect(buttonClicked)

--and more...

I want if MouseButton1Click fires, it should send more Information

Here:

local function HookButton(Target, Callback)
	local ButtonName = Target.Name
	local HookFunction = function()
		Callback(ButtonName)
	end
	return Target.MouseButton1Click:Connect(HookFunction)
end

Use:

connection = HookButton(Button1, buttonClicked)
1 Like

It says attempt to call a nil value everytime i clicked

What you could do is put a localscript inside the button and an IntValue instance.
image

Inside the localscript you do an event and you send the id of your button that you manually inserted

local ReplicatedStorage = game:WaitForChild('ReplicatedStorage')
local RemoteEventID = ReplicatedStorage:WaitForChild('GetItemIDRemoteEvent') -- your remote event
local ID = script.Parent.PartID.Value -- your button ID

button.MouseButton1Click:Connect(function()
	RemoteEventID:FireServer(ID) -- Send the ID of the button to the server
end)

Based on that you know which button is which, so in your main script you can do this

RemoteEventID.OnServerEvent:Connect(function(plr, ID)
	if buttonID == ID then
      --Code
    end
end)

Dunno if that helps

Show me how you’re using it real quick, I suspect you don’t have it setup properly.

Thanks but i need something what also Disconnects it

local function HookButton(buttonName, Callback)

local ButtonName = buttonName.Name

local HookFunction = function()

Callback(ButtonName)

end

return buttonName.MouseButton1Click:Connect(HookFunction)

end

connection = HookButton(driverButton, buttonClicked)
connection = passengerButton.MouseButton1Click:Connect(buttonClicked(passengerButton))
connection = infoButton.MouseButton1Click:Connect(buttonClicked(infoButton))

I also dont understand how this works how does connection = HookButton(Button1, buttonClicked) fire when MouseButton1Click is inside function, i didnt know that works

You have to use “connection = HookButton(buttonNameHere, buttonClicked)” on every button. Not just driverButton.

I know i dont use the others button i just did the driver Button for a test but its not working

It works by taking the Button you want to connect to, the function you want it to fire on click, and then recording the name of the button, it makes a wrapper function “HookFunction” that is called when the button is pressed. When HookFunction is called it calls “Callback” (buttonClicked function) and passes “ButtonName” which is the recorded name of your button.

1 Like

Does the function buttonClicked exist?
It doesn’t seem like it does, so that seems like the only reasonable answer.

If it doesn’t, you need to make a “buttonClicked” function for it.

How can i make a buttonClicked function, do i need to change the “HookButton”?

No, just define it separately.

So just put this somewhere between HookButton and “connection = HookButton(…)”

local function buttonClicked(NameOfButton)
  print(("Button %s was clicked!"):format(NameOfButton)
  -- Do code here. Statement above is example, you can remove it.
end