So, I have a random function that returns a random amount of variables. Is there a way to store all of these variables in a table?
example:
local randomFunction = function()
local randomVariables = {}
for i = 1, math.random(1,10) do
table.insert(randomVariables,i)
end
return unpack(randomVariables)
end
local a,b,c,... = randomFunction()
You’re already storing these values inside the randomVariables array, so just return randomVariables instead of returning the unpacked version of it. Then you only need one variable when calling randomFunction() and it will be your table.
There is no reason to unpack and after that repack your arguments if you can just index them in the array. For example if you want to obtain your “a” value then just get the first index from your new table. No need to make separate variables.
That would be pretty difficult because you would need to check for 10 different variables if they actually are a number or not and then insert them into a table. Why is it difficult? Well if you want more than 10 random variables then it starts getting really annoying to make.