Finding arguments in a function

Is there a way to find out how many arguments a function takes in? In this case the script is sent an unknown function and I need to know how many arguments there are so I don’t get an error or leave them as nil.

For example (Not the same script, but same problem):

local functions = {}

function functions.a(x, y, z)
function functions.b(x, y)
function functions.c()
function functions.d(a, b, c, d, e)


local randomFunc = functions[math.random(0, #functions)]
randomFunc() -- Unknown amount of arguments 

It looks like for your example this might be the way to go.

local functions = {
    a = {3, function blah},
    b = {2, function blah},
    c = {0, function blah}
}

If I’m being honest though, I feel like this should just be handled differently. I mean if the arguments are XYZ for some of the functions and ABCDE for others, you need to know which arguments go where if you’re calling randomFunc. It looks more to me like you should just switch to if statements and ditch the function table.

1 Like

Thanks, didn’t think of putting the amount in a table like that. Also, in my actual case I do know the order of the arguments - probably not the best example on my end, but oh well.

What you are doing is setting functions with letter names a-d,

then what you are doing is searching for functions with the name 1-4.

Instead do

local functions[1] = function(...)

end

or

function functions[1](...)

end

Edit: wrote ["1"] instead of [1]
Note, if you do want to use ["1"] use tostring() around math.random()

Thanks - again, due to a poorly made example by me