Unable to pass arguement through module scripts

Trying to pass an argument from module A to module B, however, it just prints nil in module B.

Module A

local loaded_modules = {}
local function get_module(mod_name)
	-- If unable to find module in the table
	-- require and save the module to the table
	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) 
-- In this case func_name is "Interact"
-- So it would call the "Interact" function of Module B
	local module = get_module(self.Tag.."Module")
	module[func_name](self.Instance)
end

Module B

function BooksModule:Interact(Book) -- self.Instance, which is a model
	print(Book.Name)
end

This Is For Module Script

function BooksModule:Interact(model: Model)
if model:IsA("Model") then
print("Success")
print(Model.Name)
else
warn(Model.." Is Not A Model")
end

I Think This Will Not Work As Well

in your

local function get_module(mod_name)

You need to include your local loaded modules

So try this:

local function loaded_modules.get_module(mod_name)

Does the code output any errors?

in function BooksModule:Interact(), self is passed automatically. so you should be printing self.Name instead of book.Name

1 Like

self would correspond to module B, while she is trying to print TagsController.Instance.Name(Module A)

module[func_name](self.Instance) is the same as module.func_name(self.Instance),
if you want to pass in self you need to do module[func_name](module,self.Instance)

You are right, if Instance is from module A

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