How would I print certain statements based on strings in a table

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

Do you mean the elements “Word1”, “Word2”, etc. are actually the function names? Or should be passed as arguments to a single function?

In the latter case, you can simply do this:

local func = function(word)
   print('Word: '..word)
end

for _,word in pairs(table) do
    func(word)
end
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

Nevermind, I'm blind.

No they were just an example


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?

Thank you that helped :slight_smile:

Ok, one more question. How do I insert into the table?

table.insert(t, value)

t is the table

1 Like
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

Ok, but what if the functions were in a modulescript and I had to add to the table in another script. How would I do that?

-- 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

Oh, yeah I forgot about that LOL, thank you for humoring me!
Nvm, you still gotta humor me…

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}.

1 Like

you will have to move the functions outside module.parse; the inside functions are only scoped in .parse which makes it inaccessible from other scopes

2 Likes