Running a function that is a parameter for another function

I want to make a button module consisting of hover animations and clicking animations.

function module.setUp(button, clickFunction) --Click function is the code i want to run when the player clicks the button
   button.MouseEnter:Connect(function()
      ...
   end)
   button.MouseLeave:Connect(function()
      ...
   end)
   button.MouseButton1Click:Connect(function()
      --Run clickFunction code here
   end)
end)

How can I run the clickFunction code when the player clicks the button? Do I just do:

button.MouseButton1Click:Connect(function()
   clickFunction()
end)

Help is appreciated

If clickFunction is passing the actual function then you can just do:

button.MouseButton1Click:Connect(clickFunction)

I also wanted to play the click animation along with the function

Then the way you did it should work, as well as if you want to send an argument when clicking the button (like which button has triggered the function).

since u already defined the clickFunction in setUp function argument, u can just call it there

function module.setUp(button, clickFunction) --Click function is the code i want to run when the player clicks the button
   button.MouseEnter:Connect(function()
      ...
   end)
   button.MouseLeave:Connect(function()
      ...
   end)
   button.MouseButton1Click:Connect(function()
      --Run clickFunction code here
      clickFunction()
   end)
end)

module.SetUp(SomeButton, function()
    print("Clicked", SomeButton)
end)

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