How can I get a random function within a table?

How can I get one of these two random functions? I can’t remember how to do it. Each time I try to print out the number of items in the table, it says 0. I was wondering if somebody could help me out.

local tab = {
	
	["TEST"] = function()
		
	end,
	
	["Test2"] = function()
		
		
		
	end,
}


print(#tab)
local mad = tab[math.random(1,#tab)]

print(mad)

This is because dictionaries don’t have a numerical index, if you want to randomly take a function you should instead create an array.

local Array = {
	function()
		
	end,
	
	function()	
		
	end,
}

local RandomFunction = Array[Random.new():NextInteger(1, #Array)]

One way that I would go about it would be by putting the functions into an array, and doing it as such. For example,

local tab = {
  
   abilities = {
      function(),
      function() 
   }

}

If you do need names for them, however, you can use nested arrays but this is a lot more inefficient.

local tab = {

   abilities = {
       
       {superMove = function()},
       {joe = function()}
   },

}

Is there a way to name my functions so I can remember what they are?

local Functions = {
    TEST = function()

    end,
    Test2 = function()

    end
}

function RandomValue(Table)
    local AllValues = {}

    for Index, Value in pairs(Table) do
        table.insert(AllValues, {
            Index = Index,
            Value = Value
        })
    end

    return AllValues[Random.new():NextInteger(1, #AllValues)]
end

local Function = RandomValue(Functions)

print(Function.Index, Function.Value)

with the RandomValue(Table) function you can get a random value with any table

it returns a table with the index and the value, you can see how I print them

1 Like

Maybe use comments to name them?

function() -- TEST

end,
function() -- Test2

end,

This should do fine if you’re not intending to index them by their names and only numerically (with math.random)

Otherwise it’d be worth using the method above
(which is slightly more complex but seems efficient)

While this method is more complex than the others, I think this will work well with what I am trying to do. Thanks for all the helps guys!

1 Like
local mt  = {
	__call = function(self)
		
		local i = math.random(1, # self)
		self[i]()
	end
}

local functions = setmetatable({
	function ()
		print("First function")
	end,
	function ()
		print("Second function")
	end,
	function()
		print("Third function")
	end,
}, mt)

while true do
	functions()
	wait(0.75)
end

Here is a method you could take, using meta-tables. Whenever you want a random function selecting just call functions() as if was a function.

2 Likes
local t = {
    --This is test 1 function
    [1] = function()
       return "Test 1" 
    end;
    --This is test 2 function
    [2] = function()
       return "Test 2" 
    end
}

print(#t)
--2
local tab = t[math.random(1, #t)]

print(tab())
--Test 2

This is what I would do if I need to do this, and you could just name the function by leaving a comment, or you can do this:

--Name of the functions
local name = {
    "Test1";
    "Test2"
}
--Table of the functions
local t = {
    ["Test1"] = function()
       return "Test 1" 
    end;
    
    ["Test2"] = function()
       return "Test 2" 
    end
}

print(#t, #name)
--0,  2

local tab = name[math.random(1, #name)]

print(t[tab]())
--Test 2

Either way both works