Calling a function in another module script using a table of stored functions

I’ve been messing around with collection service and seeing how I could implement it in a practical manner
In this situation, some requirements in a local script are met, this calls the tagcontroller module and passes through the func arguement which is a string (such as “interact”)

TagController Module

local FunctionsTable = {
	["Interact"] = Interact,
	
}

function TagsController:CallFunction(func)
	-- script:FindFirstChild(self.Tag.."Module") should get the other module script
	-- :FunctionsTable[func]() should call the function in that module script
	script:FindFirstChild(self.Tag.."Module"):FunctionsTable[func]()
end

However, this line script:FindFirstChild(self.Tag.."Module"):FunctionsTable[func]() doesn’t seem to be correct, what am I doing wrong?
image

There are a few things that could be causing the issue in the line of code you provided. Here are a few potential issues:

  1. The FindFirstChild method might not be finding the correct child object. Make sure that the object you are trying to find exists in the model and that you are searching for it by the correct name.
  2. The FunctionsTable table might not have the key that you are trying to access. Make sure that the key you are trying to access exists in the table and that you are spelling it correctly.
  3. The function that you are trying to call might not exist or might have a typo in its name. Make sure that the function you are trying to call exists and that you are spelling its name correctly.

It’s also worth noting that this line of code could potentially cause a runtime error if any of the objects or functions being accessed do not exist. You might want to consider adding some error checking to your code to handle these cases.

Yeah no it’s definitely none of these things, did you look at the screenshot I posted?

The part where it says script:FindFirstChild(self.Tag.."Module") should return an object.
If that’s a module script maybe you want to do this?

require(script:FindFirstChild(self.Tag.."Module"))

Or maybe you’re trying to do this:

FunctionsTable[func]()

Either way I no idea what you’re trying to do.

I was going along with this post, however, I want to call functions that are in another module script. This doesn’t seem possible though?

Bump

-- server script
local TagsController = {}

local loaded_modules = {}
local function get_module(mod_name)
    if not loaded_modules[mod_name] then
        loaded_modules[mod_name] = require(script:FindFirstChild(mod_name))
    end
    return loaded_modules[mod_name]
end
function TagsController:CallFunction(func_name)
    local mod = get_module(self.Tag.."Module")
    return mod[func_name]()
end


-- at some point
TagsController.Tag = "DoAThingy" -- module name is DoAThingyModule
TagsController:CallFunction("DoAThing") -- function name is DoAThing

in module:

-- module of some sort
local module_functions = {}

function module_functions.DoAThing()
    print("do thing")
end

return module_functions

The problem with your suggestion is func_name is a string. So calling the function of that name would just return an “attempt to call nil value” error.


It works.
The reason it works is because mod, local mod = get_module(self.Tag.."Module"), is a table. To index a table, you can use a string like so: mod[func_name]. In this case, the indexed value is a function, which you can call using parentheses like this: mod[func_name]()

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.