What I want to accomplish is basically running through a table in order, and running functions based on the string in the table. Here is an example.
local table = {"Word1", "Word2", "Word5", "Word8", "Word1"}
The script should go through and execute a function based on the strings in the table, and what order they are in.
I know there is probably a really simple solution I am not seeing, but it’s been a long day lol
local function func1()
print("Function 1")
end
local function func2()
print("Function 2")
end
local function func3()
print("Function 3")
end
local function_table = {func1, func2, func3}
-- Loop through the table and execute each function
for _, func in pairs(function_table) do
func()
end
Great, I think that will work. Is there any way I could put those functions (func1, etc.) inside a modulescript and have the function table be inputted through a different script?
local function func1()
print("Function 1")
end
local function func2()
print("Function 2")
end
local function func3()
print("Function 3")
end
local function_table = {func1, func2, func3}
table.insert(function_table,func1)
-- Loop through the table and execute each function
for _, func in pairs(function_table) do
func()
end
-- Load the module from other script
local MyModule = require(game.ServerScriptService.ModuleScript)
local function func1()
print("Function 1")
end
local function func2()
print("Function 2")
end
local function func3()
print("Function 3")
end
-- You can add both local and module fucntions
local function_table = {func1, func2, func3,MyModule.func2}
table.insert(function_table,MyModule.func1)
-- Loop through the table and execute each function
for _, func in pairs(function_table) do
func()
end
Ok, on further testing It seems it doesn’t work. For some reason table.insert doesn’t insert MyModule.func1. Any reason why?
Edit for clarification:
This is what is not working. Here is the code for the localscript:
local module = require(game.ReplicatedStorage.Module)
local tayble = {} --table misspelled on purpose
table.insert(tayble, module.func1)
print(tayble)
module.parse(tayble)
And in the modulescript:
local module = {}
function module.parse(blocks)
print(blocks)
function module.func1()
print("Function 1")
end
function module.func2()
print("Function 2")
end
function module.func3()
print("Function 3")
end
for _, func in pairs(blocks) do
print("Parse")
func()
end
end
return module
When I print “tayble”, it outputs {}, and nothing inside of it while it should output {module.func1}.