ModuleScript not working

trying to use OOP to make it spawn parts but it wont work for some reasons there are no errors aswell any help?

script:

local module = {}
module.Entries = {}
module.maxamountofentries = math.random(5,10)

function module:CreatePart(part)
	local parttable = setmetatable({},self)
	parttable.__index = parttable
	parttable.Part = part
	return parttable
end


function module:CloneAmountOfParts()
	local part = self.part:Clone()
	part.Parent = workspace
	part.Transparency = 0
	    print("cloned")
		table.insert(self.Entries,1,part)
		
end

function module:RemoveTooManyPart()
	for i,v in ipairs(self.Entries) do
	       print(i,v)
		end
	end


return module


Ok so.
It works, but.
You need to do this.
Define metamethod,
and .CreatePart() not :CreatePart()

local module = {}
module.Entries = {}
module.maxamountofentries = math.random(5,10)
module.__index = module

function module.CreatePart(part) -- call .CreatePart not :CreatePart
	local parttable = setmetatable({},self)
	
	parttable.Part = part
	return parttable
end


function module:CloneAmountOfParts()
	local part = self.part:Clone()
	part.Parent = workspace
	part.Transparency = 0
	    print("cloned")
		table.insert(self.Entries,1,part)
		
end

function module:RemoveTooManyPart()
	for i,v in ipairs(self.Entries) do
	       print(i,v)
		end
	end


return module

It doesnt work i dont know if im calling it wrong

local modulescript = require(game.ServerScriptService.OOPmODULEtEST)
local part = script.Part 
while wait(1) do
	modulescript:CreatePart(part)
	print("done?")
	end

is this how you call it?

.CreatePart not :CreatePart
I told you before

Um you removed the __index and one of my functions has to access the metatable to get access to the part and part’s properties. And therefore it doesnt work…

Is there any other way to fix it?

And if i add the index back it errors

You’re using self in methods where it isn’t defined?
Self exists within methods, correct me if I’m wrong but is it not there to reference the first parameter?
CreatePart seems to be fine but your metatable definition looks wrong.

Try this:

function module:CreatePart(part)
    local parttable = setmetatable({}, {
        __index = function(t, k)
            -- usage here
            return t, k
        end
    })

Remember, the metamethod __index will only fire when a nil value is indexed within the table. I don’t see why you need a metatable for this anyway but whatever.

I mean i put self as the first argument and ive read that its supposed to define that.

and ive also read that __index is used to construct a metatable.

It isn’t, it’s a metamethod lol.
There are over 10+ metamethods in rlua that you can use or combine to construct a metatable.

And no, you need to specify an argument within the parameters first I’m pretty sure, self will then assume that argument’s position.

Have you tried using LogService to find the error?
Or applying pcall() inside the functions?

I mean i did specify self as the first argument i just didnt add that in the code yet because i used : instead of .

It wont let me return the table. Keeps coming out nil. Also tried creating another metatable inside that function and nothing worked. And tried putting the table as an argument and that did not work aswell. Very confusing stuff.

The issue is with your CreatePart method; as previously suggested, it should be .CreatePart, but as this is your constructor, self is not yet defined.

Here’s how your module should look:

local module = {}

module.entries = {}
module.maxamountofentries = math.random(5, 10)

-- define the "__index" metamethod outside of your constructor
module.__index = module

-- "self" doesn't exist yet, so we use the constructor to
-- create it by linking "module" to an empty table as its
-- metatable -- typically, you'd call this method `.new`
function module.CreatePart(part)
    local self = setmetatable({}, module)
    self.part = part

    return self
end

-- now we can use a colon for methods, which
-- automatically injects "self" as a variable
function module:CloneAmountOfParts()
    local part = self.part:Clone()
    part.Transparency = 0
    part.Parent = workspace

    print("Cloned to Workspace")
    self.entries[#self.entries + 1] = part
end

function module:RemoveTooManyParts()
    for idx, part in next, self.entries do
        print(idx, "=", part)
    end
end

return module
1 Like

local module = {}
module.__index = module
module.Entries = {}
module.maxamountofentries = math.random(5,10)

function module.CreatePart(part) – call .CreatePart not :CreatePart
local self = setmetatable({}, module)

self.Part = part
return self

end

function module:CloneAmountOfParts()
local part = self.part:Clone()
part.Parent = workspace
part.Transparency = 0
print(“cloned”)
table.insert(self.Entries,1,part)

end

function module:RemoveTooManyPart()
for i,v in ipairs(self.Entries) do
print(i,v)
end
end

return module

– Requiring it!

local part = script.Part
local modulescript = require(game.ServerScriptService.00Pm0DULEtEST).CreatePart(part)

while true do
wait(1)
modulescript:CloneAmount0fParts()
print(“done?”)
end

It still does not work without any errors.

Is it because where the script is?

My script is in serverscriptservice.

I am calling it right.

And no “ModuleScript has experienced an error while loading”.

And im calling it like this

local module = require(script.Parent.OOPmODULEtEST)
local part = script.Part

while wait(1) do
	module.CreatePart(part)
end

When calling :CloneAmountOfParts() its nil. When the part in self is defined.

Any idea why??

I don’t know how to fix this but everytime I try to use module scripts I get an extra first argument that I’m not using, so try changing function module:CreatePart(part) to function module:CreatePart(_, part)

please don’t do this for creating a part, it’s dumb D: