I’m trying to create objects for my game, but each time new is called it always will either return the exact same table (self) or it will return a table without any of the functions I included in the module! For simplicity’s sake I tried stripping the module down to just this.
local module = {}
module.__index = {}
function module:new()
print(tostring(self))
return self
end
function module:move()
warn(tostring(self))
end
return module
The issue persists either way! Here is an image of what is being printed by the way.
So maybe I just have to make a new table each time right? So I tried using this code:
function module:new()
self = setmetatable({}, module)
…and even…
function module.new()
self = setmetatable({}, module)
And now each table being printed is different, But they are completely empty!! I genuinely have no clue what I can do to fix this, I tried searching everywhere on the devforum and I had no success. Heres whats being printed after inserting the previous code by the way.
First off when making a class, it should always be with a dot, next you have to specify parameters inside the class, for example the string you want to print, and then make a method to print said string with a :
So a program that prints a string specified in the class might look something like this.
local module = {}
module.__Index = module
function module.new(TheString)
local self = setmetatable(module, {})
self.String = TheString
return self
end
function module:PrintString()
print(self.String)
end
return module
Look up some OOP guides on YouTube, they will help you understand.
This unfortunately did not solve my issue, I added your change into my code and the issue still persists.
local module = {}
module.__index = {}
function module.new()
local self = setmetatable(module, {})
print(tostring(self))
return self
end
function module:move()
warn(tostring(self))
end
return module
Notice how all the tables being printed are the exact same.
I’ll give you both actually, here is the “stripped down” version I’m using to debug this issue.
local module = {}
module.__index = module
function module.new()
local self = setmetatable({}, module)
print(tostring(self))
return self
end
function module:move()
warn(tostring(self))
end
return module
Here is what I wrote before the extensive debugging.
local object = {className = 'coin'}
--object.__index = object
local mod = script.Parent.Parent
local helpers = require(mod.helpers)
local visuals = require(mod.visuals)
function object:destroy()
-- destroy the object
self.visual:destroy()
--self = nil
end
local test = 0
function object:new(parent, position: Vector3)
test += 1
self.test = test
-- create the model for this object
local visual = visuals.coin:new()
self.visual = visual
if position then
visual:move(position)
end
--
return self
end
--
function object:move(position: Vector3)
self.visual:move(position, self.test)
return self
end
return object
local object = {className = 'coin'}
object.__index = object
local mod = script.Parent.Parent
local helpers = require(mod.helpers)
local visuals = require(mod.visuals)
local test = 0
function object.new(parent, position: Vector3)
test += 1
local newObj = setmetatable({}, object)
newObj.test = test
-- create the model for this object
local visual = visuals.coin.new()
newObj.visual = visual
if position then
visual:move(position)
end
--
return newObj
end
--
function object:move(position: Vector3)
self.visual:move(position, self.test)
return self
end
--
function object:destroy()
-- destroy the object
self.visual:destroy()
--self = nil
end
return object
Try this one and let me know if it solves your problem.