"Button.MouseButton1Up:Connect(OnClick)(Button)" doesn't work in the way i thought it would

“Button” in the local function always returns nil
image

not really a problem, as i can do:

Button.MouseButton1Up:Connect(function()
   OnClick(Button)
end)

but my monkey brain wants to see less lines. for now ill just do the latter

image

That’s not the way it works; you can’t pass parameters to Connect or Once methods using this syntax (Although, in this case, you’re actually calling RBXScriptConnection, which would error)

i figured it wouldve worked like that given coroutine.wrap(X)() does.

Those methods behave differently, and thus have that ability. The closest thing you can get to what you want is this:

local function OnClick(button)
   return function()
      ---... this is the actual function being executed by the connection
   end
end

--...
Button.MouseButton1Up:Connect(OnClick(Button)) -- this will run the result of 'OnClick'
--...

But this can increase memory usage if used too much

i don’t plan to use it, but it’s nice to know its an option

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