Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago

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

Log in to vote
1
Answered by
Speedmask 661 Moderation Voter
2 years ago
Edited 2 years ago

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.

Ad
Log in to vote
1
Answered by 2 years ago

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

0
primitive type is still faster. by like, irrelevant speeds, but I still use it to check primitives anyways :) Speedmask 661 — 2y
Log in to vote
0
Answered by
NGC4637 602 Moderation Voter
2 years ago

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)

Answer this question