Passing multiple arguments to a function with a connect, is it possible?

Hey!
Imagine you have a touched event (or something else) but you have to pass the hit and the name of the baseplate to it (for whatever reason), how would you do it?

I would think of this:

local function touch(hit, name) 
    print(hit.Parent.Name .. " touched " .. name)
end

workspace.Baseplate.Touched:Connect(function(hit)
    touch(hit, workspace.Baseplate.Name)
end)

But isn’t there a way where you could do something like this

local function touch(hit, name)
   print(hit.Parent.Name .. " touched " .. name)
end

workspace.Baseplate.Touched:Connect(touch(workspace.Baseplate.Name)) --Hit automatically gets passed as first arg

This would obviously be better performance-wise and save one extra undeclared function.
My example might not be the best but you probably get what I mean.
Thanks!

I’ve never tried it myself but I might work just test

No, it doesn’t. It throws an error.
It’s kinda sad as making an extra function just to call it is kinda bad practice in my opinion and worse performance-wise.

1 Like

RigidStudios#6315 answered my question on discord, so thanks to him.
You can’t do that but you can make a function that does that.

His code:

--customConnect(RBXScriptSignal, func, ...extraParams)
function customConnect(RBXScriptSignal, func, ...)
  while true do
   func(RBXScriptSignal:Wait(), ...)
  end
end

function touch(toucher, touchee)
  --...
end

customConnect(workspace.Baseplate.Touched, touch, workspace.Baseplate.Name)
3 Likes