Why are my parameters not carrying across functions properly?

The following code snippet is simplified code from a returned function within a ModuleScript that I intend to use for error handling:

function errorHandling(func, ...)
	local args = {...};
	print(unpack(args));
	local ran,error = pcall(function()
		print(unpack(args));
		local results = { func(unpack(args)) };
	end);
end

And the following code is the function that is being passed to errorHandling as func:

function func(param1, param2)
	print(param1, param2);
end

Now the issue I have is that the first two times I print the unpacked values of the args array it returns the correct values, however when I receive them in the func function and print, the first index has disappeared.

For example, if my arguments (args) are “a” and “b”, my output will show:

a	b
a	b
b	nil

I’ve not been scripting for a few years so it’s probably something simple I’m missing, but I’d appreciate the help.

Update: When I change func(unpack(args)) to func(nil, unpack(args)), the correct parameters arrive in func.

3 Likes

Could you please show a screenshot of what you’re trying to do? I was not able to reproduce your issue.
This is what I did :
image

This is what the module script has :
image

Gyazo Link

There might be something else that I’m missing, but I’m running ‘_errorHandling’ with the arguments:

func [function]
a [string]
b [string]

and when I print the passed parameters from after the function I am only receiving ‘b’

That’s because you’re using namecall “:” when you defined the function but then you’re indexing when you call it “.”.

If you change it to

require(game.Selection:Get()[1]):_errorHandling(func,"a","b")

It should work again.

When you define a function with : and call it with . you have to pass self (_errorHandling in this case), you should look more into metatables and just calling functions in general.

(I’m bad at explaining sorry)

3 Likes

That seems to have solved it. Thanks for the help.

2 Likes