Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can you put functions on a table?

If so how would I be able to call a function from a table?

1 comment

0

So you mean, having a table with a function in it, and being able to call the function from the table?

2 answers

✓ Accepted answer

That depends on your definition of putting functions in a table. I'll give you an example:

function hi()
	print('hi')
end

function bye()
	print('bye')
end

local my_Table = {hi,bye}

my_Table[1]()

The last line of the above code will call the function of the first parameter of the table, being hi. To my knowledge, that's the only way you can store functions in tables, and still be able to call them. I can' give you a guarantee that this will work with calling a function on a global table, as I've never tried it before. If you have any questions, feel free to comment!

1 comment

0

Beat me too it haha

You can store functions inside tables.

To do this, simply define your function like you would any other time and insert the function into the table:

local function aFunction(args)
	print("Hello World!")
end

local array = {aFunction}

Now if you want to call this function, you'll have to retrieve it from the table and then slap the call operator/method (whatever the correct terminology is) onto the end:

local array = {aFunction}
array[1]()
--[[
	Output:
	Hello World!
]]

1 comment

0

I'm a ninja in the shadows! Love your answer, though.

This discussion is preserved as it was. New answers, comments, and votes are closed.