Problems with parameters

Module Script:

Functions.Event = function(event, instance, callback, ...:any)
	local event = instance[event]
	local args = {...}
	
	event:Connect(function(...:any)	
		callback(table.unpack(args), ...)
	end)
end

Local Script:

local BindableFunction = function(rollframeclone, Heartbeat, tween1, tween2, roll)
	local childrens = rollframeclone:GetChildren()
	for _,v in pairs(childrens) do
		if not v:IsA("TextButton") then continue end
		Event("Activated", v, v_Ativado, v, tween1, tween2, Heartbeat, rollframeclone, roll)
	end
end

local v_Ativado = function(v, tween1, tween2, Heartbeat, rollframeclone, roll)	
	local nome = v.Name
	local bindable = bindablefunction:Invoke(nome, roll)

	local escolhido = rollframeclone[t[not bindable]]
	local nomeescolhido = tostring(escolhido)
	
	local deletadoescolhido = WaitFor(rollframeclone, nomeescolhido)
	v.Active = false
	
	for numero = 0, 1, 0.1 do
		task.wait(0.05 + Heartbeat:Wait())
		deletadoescolhido.TextTransparency = numero
	end
	
	coroutine.wrap(function()
		while Heartbeat:Wait() do
			task.wait(0.3)
			v.TextColor3 = Color3.new(1, 1, 0.411765)
			task.wait(0.3)
			v.TextColor3 = Color3.new(255,255,255)
		end
	end)()
	
	Destroy_Instance(deletadoescolhido)
	Tween_Audio_Play(tween1)
	Completed(tween1, Tween_Audio_Play, tween2)
end

My problem here is: Heartbeat, rollframeclone, and roll are returning nil when it is passed onto the “Event” function.

I tried printing it before passing it to the “Event” function and it prints the normal value. When I removed callback(table.unpack(args), ...) with callback(table.unpack(args)) it worked.

What is the problem here?

What I’m trying to reach is to pass arguments of the Event:Connect(). Some events don’t have arguments, so sometimes the table would be nil, nothing. Is it the problem?

Due to how ... varargs works, you are replacing all the arguments from the second one onwards. Try this to see what I mean:

print(table.unpack({1, 2, 3}))
1 2 3
print(table.unpack({1, 2, 3}), 4, 5, 6)
1 4 5 6

Wait, what do you mean exactly? I’m sorry, I’m learning scripting, but I still consider myself a novice. Could you explain better? I’m replacing the first vararg with the second one?

Wait, I forgot I cannot put a tuple of arguments before the last index, is that it then?

local function a(..., b) -- you can't do this

... at the end of a function signature will capture zero or more extra arguments beyond the “named” ones to the left of it. You can pass this on to other functions or pack it with {...} so you can access each one separately. What you’re doing makes sense if having two ... places them side by side, but it cannot work that way due to some technical reasons in the language. You can work around it by doing something like this:

local args = {}

event:Connect(function(...:any)	
	local newargs = {}
	for i, v in pairs(args) do
		newargs[i] = v
	end
	for i, v in pairs({...}) do
		newargs[#newargs + 1] = v
	end
	callback(table.unpack(newargs))
end)

Correct you cannot do this. Its because it does not actually work like a tuple or array.

1 Like

so you are like adding the first ... with the second ...? What if the second vararg actually doesn’t have a value, since not all of the default events of the roblox contain pre-defined arguments?

My example will always combine then left to right. If the second ... is empty the loop will run zero times and so newargs will just be args.

1 Like

so you are like combining both args? like {…} + {…}

I think I understand what you are doing here. Thank you!