Connect a local function to an event while passing arguments

Hello! I’m trying to to connect a local function to an event while also passing arguments. I’m not really sure how to format this. I also didn’t know what to look up in order to find an answer on google (I presumed it was already solved somewhere… seems kind of basic)

local function sideButtons(n)
     print(n)
end

badgeCategories.Parent["-1"].MouseButton1Click:Connect(sideButtons)(-1)

Any help? Thanks in advance!

I’m not sure if you could do it like that but you could always do;

badgeCategories.Parent["-1"].MouseButton1Click:Connect(function()
    sideButtons(-1)
end)
1 Like

Yeah, I already figured out that method. I was hoping there was a way without creating another function when one already exists.

Thank you for replying so fast; though I’ll keep the post active to see if there’s another way, you have a very good alternative.

Yeah I just checked and as I thought that’s the most efficient way to do it, no way to do it with 1 line, sadly.

1 Like

.MouseButton1Click passes no parameters. Instead, you should save it as a local variable and print it from there:

local n = -1
local function sideButtons()
     print(n)
end

badgeCategories.Parent["-1"].MouseButton1Click:Connect(sideButtons)

badgeCategories.Parent:WaitForChild("-1").MouseButton1Click:Connect(function()
SideButtons(argument here)
end)

badgeCategories.Parent["-1"].MouseButton1Click:Connect(function() sideButtons(-1) end)

Functions don’t need to span multiple lines (you likely know this).

2 Likes