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

How to get a table of functions that are inside a module script?

How would you grab all the functions of a module script and put it into a table.

Heres something I tried but It wouldn't work!

for i, v in pairs(plateModule) do
			if (type(v) == 'function') then
				if i == math.random(1, 4) then
					v(player, plate)
				end
			end
		end

3 answers

✓ Accepted answer

you can actually do it automatically like this

for i, v in pairs(getfenv()) do
    if (type(v) == 'function') then
        if i == math.random(1, 4) then
            v(player, plate)
        end
    end
end

but unless it’s for fun... please NEVER do this. the way others have suggested is far better, because doing this has a lot of problems if you could not guess already.

Can't you directly assign the functions inside of the module script like this?

function a()
end

function b()
end

return {

	funs = {a, b}

}

Like this you could get a table of functions from another script by just doing

local funs = require(moduleScript).funs

Also, use "typeof" instead of "type". Type only supports vanilla lua types while typeof includes all robloxs classes

1 comment

0

primitive type is still faster. by like, irrelevant speeds, but I still use it to check primitives anyways :)

you can do this in the module script

local module = {}

function module.function(args,args,args) -- can be module:function() too
	-- your function here
	return (what you wanna return here, delete this line if you dont wanna return anything)
end

function module.function2(args,args,args) -- btw, args stands for arguments aka parameters.
	-- i think you know what am doin
	return (what you wanna return here, delete this line if you dont wanna return anything)
end

return module

then, you can use that function in serverscripts and local scripts like this:

local module = require(insert module script here)

module.function(args,args,args)

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