How To Pass Parameters Through One Line Connect

Hello all! I had a question on how to be able to pass specific arguments through this one line of code, since I have never done arguments this way. It is necessary to know the name of the instance, and I could not figure out any other way to be able to find it out. Thank you so much!!

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ClickAnimsScript = require(ReplicatedStorage.ButtonClickAnims)

local collectionService = game:GetService("CollectionService")

local connection = {}

local function clickButton(name)
	warn(name)
	
	game.ReplicatedStorage.DistributionCodes:FireServer("Claiming",name)
end

script.Parent.ChildAdded:Connect(function(instance)	
	warn(#collectionService:GetTagged("DistributionItems"))
	
	warn(instance.Name)
	
	connection[instance] = instance.MouseButton1Click:Connect(clickButton)
end)

script.Parent.ChildRemoved:Connect(function(instance)
	if connection[instance] then
		connection[instance]:Disconnect()
		connection[instance] = nil
	end
	
	warn(#collectionService:GetTagged("DistributionItems"))
end)

Can you elaborate, please? Which line of code you’re talking about?

This one, since it triggers the function and I cannot seem to find a way to attach an argument that can specify a “name” as shown in the clickButton function.

Oh, you want to pass the instance argument to the clickButton function?
Since you cannot pass additional arguments to the :Connect() function, you can create an anonymous function that calls the clickButton function with the instance.
Something like this:

connection[instance] = instance.MouseButton1Click:Connect(function()
   clickButton(instance.Name)
end)
2 Likes

Thank you so much for your help, it’s greatly appreciated!

1 Like

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