How would I fix this and do what the title says?
local function yay()
print("yay")
end
local function grr()
print("grr")
end
local functions = {yay(), grr()}
functions[math.random(1,#functions)]() -- this doesn't work
Please help.
How would I fix this and do what the title says?
local function yay()
print("yay")
end
local function grr()
print("grr")
end
local functions = {yay(), grr()}
functions[math.random(1,#functions)]() -- this doesn't work
Please help.
Your code is mostly correct, just change the table to this:
local functions = {yay, grr}
The reason why you should remove the parenthesis is because you just want to write the reference to the function, the parenthesis call the function
In addition to what @HugeCoolboy2007 mentioned,
If you want to store some functions in this way , you could
local functions = {
["Yay"] = function()
print("Yay2")
end,
["Grr"] = function()
print("Grr2")
end,
}
for Name,Value in pairs(functions) do
print(Name) -- "Yay", "Grr"
Value() --> "Yay2","Grr2"
end
local functions = {}
function functions.Yay()
print("Yay2")
end
function functions.Grr()
print("Grr2")
end
and the modularized way.
You’re right man, love your notes, always infortmative.