Calling a function from the same module script

Whenever certain conditions are met, I want a function to call a method from the same module script.
The problem is when the method is called, the object doesn’t seem to exist anymore, as self returns with a table of the functions in the module script, and not the contents that were set with Module.new().

What self should contain
image

What self contains when called from the function
image

Example code of what I’m doing

function Module:Method()
-- do stuff
end

local function CheckRequirments()
    if met then
        Module:Method()   -- This doesn't work :(
    end
end

For it to access the data, you must pass self the table returned by the module as an argument

local function CheckRequirments(self)
    if met then
        self:Method()
    end
end

you must save it or create a new function in the module that gets the table, here is an example with a construction system

local module, Storage = {}, {}
module.__index = module

function module.new(Name)
	local Part = Instance.new("Part")
	Part.Name = Name
	Part.Parent = workspace
	
	local New = setmetatable({}, module)
	New.Part = Part
	Storage[Part] = New
	return New
end
function module.get(Part)
	return Storage[Part]
end

return module
1 Like