`self` is always returning the exact same table

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.
image
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.
image

2 Likes

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.

1 Like

you need to do this
local self = setmetatable({}, module)

1 Like

I tested it in studio, it gives me an error if I do it like that.

1 Like

worked fine for me
char char char

1 Like

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

image
Notice how all the tables being printed are the exact same.

1 Like

can you try
local self = setmetatable({}, module)

2 Likes

I guess because mine is in a module script, not a server script.

1 Like

The tables are all different but they are completely empty this time.
image

1 Like

you shouldn’t be setting it to a new table you should be setting it to the table of the module or script your using. Like this

module.__index = module

3 Likes

Thank you for pointing this out! I’m honestly a bit tired from all of this debugging I’ve been doing.
This however didn’t solve my issue.
image

1 Like

btw you typed module.__Index = module wrong

here that gives error

1 Like

Can you post the full version of your code again?

1 Like

can you try call move function like self:move()

1 Like

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
1 Like

The move function does not exist in the table.

1 Like

is it gives an error?
it should work because it is searching in module table not your new table

2 Likes
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.

3 Likes

This did not solve my problem unfortunately, the tables are all missing their functions.

1 Like

I forgot to screenshot the console!
image

1 Like