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
local modulescript = require(game.ServerScriptService.OOPmODULEtEST)
local part = script.Part
while wait(1) do
modulescript:CreatePart(part)
print("done?")
end
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…
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.
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
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
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)