How can I store a tuple?

I am trying to create a simple wrapper for pcall to eliminate duplicated patterns:

local function Call(Func, ...)
	local Success, Return = pcall(Func, ...)
	if Success then
		return Return
	end
	warn(Return)
	return nil
end

However, if Func returns multiple values, all except the first are discarded. How can I fix this?

I believe this would work:

local Success, ... = pcall(Func, ...)

if Success then
	return ...
end
warn(Return)
return nil

That does not work for me, sorry.

Maybe try this?

local Success,  Return = pcall(function()
-- whatever here

end)

if Success then
return
warn(return)
else
error()
end

You can pack the results of the function into a table, then return the contents of the table. Something like this:

local function Call(Func, ...)
	local Return = table.pack(pcall(Func, ...))
	local Success = Return[1]

	if Success then
		table.remove(Return, 1)
		return table.unpack(Return)
	end
	warn(Return[2])
	return nil
end
1 Like