Can I pass additional arguments with the touched event while connecting a function with multiple parameters?

I was wondering if it was possible to pass additional arguments into a function connected to a touched event? Basically, can I use a predefined function instead of simply having to connect a new function?

local function SendPlayer(player, intValue)
	print("Worked")
end

Part.Touched:Connect(SendPlayer) 
--Is it possible to somehow pass in the additional intValue?
4 Likes

No. The engine passes the arguments to your event listeners. You can’t provide your own arguments. You will need to pass them yourself.

part.Touched:Connect(function(other)
    SendPlayer(player, intValue)
end)
19 Likes

Thank you, this worked for me!

part.Touched:Connect(function(player)
    SendPlayer(player, intValue)
end)
2 Likes

While that works; for anyone who may fall across this post in the future, nameless functions are bad practise. While…okay…for this use another alternative is:

local function SendPlayer(player, intValue)
    return function(other)
	    print("Worked")
    end
end

Part.Touched:Connect(SendPlayer) 
4 Likes
Original Reply

With your alternative, I can not see where and how the arguments from SendPlayer(player,intValue) will be passed with :Connect(SendPlayer)? Anonymous (nameless) functions inside a connection seem to be the only way. I would gladly like to avoid but it doesn’t seem possible. Even in your alternative that is a nameless function after the return.
Edit – After a few thread searches I think this is what you were trying to achieve:
Alternative for cleaner code

local function SendPlayer(player,intValue)
    return Part.Touched:Connect(function()
        print("Worked")
    end)
end

SendPlayer(player,intValue)

This indeed will make reused functions with arguments for connection events possible and more presentable in code. (I don’t know about efficiency in comparison though)

–Thus, cheers to any more future searchers (like me).

Major Edit Upon receiving :heart: on my reply, I decided to update my post as I now have a bit more experience scripting. Anonymous functions are not a bad practice and the :white_check_mark: marked solution is the sane way to do it! However, while it is possible to pass arguments inside a .Event:Connect(NamedFuntion(Agr1,Arg2)) that is bad practice. To repurpose an RBX signal (i.e. Touched Event) just to pass custom arguments to be what seems visually appropriate is not systematic.

Properly use defined function with Touched Event
-- our defined function
local function ThingsToDo(Player,intValue)
    --send value to player...
end
-- prestructured function from Touched Event
local function OnPartTouched(WhatTouched)
    -- define variables
    local PlayerObject = Players.Username
    local IntegerValue = intValueObject.Value
    -- use variable arguments
    ThingsToDo(PlayerObject,IntegerValue)
end

Part.Touched:Connect(OnPartTouched)
1 Like

Here’s the working code although I don’t use this method any more:

local function SendPlayer(intValue)
	return function(player)
		print(player)
		print(intValue)
	end
end

local intValue = 3
part.Touched:Connect(SendPlayer(intValue))
2 Likes