Pass a variable with :Connect(function)

Quick example:

function hello(a, b) -- Function takes 2 variables
     print("Touched:",a,"something:",b)
end

for _,v in ipairs(objects) do
   local myVariable = v.Something.Value
   v.Touched:Connect(hello) -- How would I pass "myVariable" to function "hello"?
end
2 Likes

You can’t. You’ll have to connect a function that calls the function with the parameters you want:

v.Touched:Connect(function(...)
    hello(myVariable, ...)
end)
9 Likes

Okay, thank u!

Character limit

Isn’t There A Shorter Way Of Sending Variables Without Creating A Whole New Function?

1 Like

you can also do it like this which is basicly the same, but most people just do it like this bc it looks nicer


function hello(a)
   return function(b)
      print(a.." and "..b.." touched!")
   end
end

for _,v in ipairs(objects) do
   local myVariable = v.Something.Value
   v.Touched:Connect(hello(myVariable))
end