Hey. I’m stumped on this topic, even though I’m thinking it should be simple. What I need to accomplish is to call a function (which is in a table) through indexing. If someone could help me with this, that’d be much appreciated!
How the function looks in the table:
local Table = {
foo = function(x)
end;
}
How I’m currently trying to call it:
Table[math.random(#Table)](x) -- Randomised index
Table[1](x) -- Predetermined index
you cannot get a random number between nothing and #Table, there have to be two (integer) bounds while using math.random; try
– but math.random(#t) works in cases where t = not 0 because while doing math.random(#t) , 1 is automatically passed as the first bound
local Table = {
foo = function(x)
return print(x)
end;
}
local m = math.random(1, #Table)
Table[m]("abc")
I’m pretty sure you can, as I’ve done it before previously when indexing a table with math.random with normal variables. I’ll try doing it your way and see if it changes anything.
Edit: Tested, only prints the first one in a table with 3 values.
If I’m not mistaken pairs and/or ipairs uses next to iterate through it to get the values.