Not Able To Find A Set Table Inside Of "self"

Hello, as said in the title I am attempting to index a value of “self” below, and the value is returning nil, I’m not entirely sure why and can’t seem to find a solution.

Code:

local aTable = {}
local metatable = {__index = aTable}

function aTable.initialize(MinSpeed, MaxSpeed, AccelIncrement)
	local self = {}
	setmetatable(self, metatable)

	-- Values --
	self.SetValues = {}
	self.SetValues.MIN_SPEED = MinSpeed
	self.SetValues.MAX_SPEED = MaxSpeed
	self.SetValues.ACCEL_INCREMENT = AccelIncrement
	self.SetValues.GRAV = workspace.Gravity
	--
	print(self)
	return self
end
--
function aTable:start(target)
	print(self.SetValues) -- Prints "nil"
end

Any help appreciated

1 Like

are you running start before initialize?

No, I initialize first, then run table:start.

isnt table a metatable?

like table.insert

roblox might not be very happy with changing that

Yes, the table isnt actually called that, I just changed the name when i was posting it on the devforum, ill edit it

If I print “self” in aTable:start, it just prints both function names, nowhere is there the table I returned with the “initialize” function

this is pretty weird

im gonna try do some testing

Alright, much appreciated

minimum

ok this probably has something to do with script context

Where are you calling the start method?

after the initialize

tryna debug it

Might have something to do with this line, then?
Try replacing it with aTable._index = aTable and do setmetatable(self, aTable) instead of setmetatable(self, metatable)

If you want to know how the server script looks, it looks like this

local module = require(script.MissileModule)
--
local target = workspace:WaitForChild("LookAt")
--
local MinSpeed = 10
local MaxSpeed = 200
local AccelIncrement = 2
--
module.initialize(MinSpeed, MaxSpeed, AccelIncrement)
task.wait(5)
module:start(target)

[“_index”] = “*** cycle table reference detected ***”,

roblox not happy

still returning nil, in the initialize function, self prints just fine, but in the other one it acts like theres no table

Are you getting any errors, the code you had originally works for me in studio?

oh i fixed it on accident lol

min characters

nope, theres no errors, I dont know what the issue is

brute force

aTable = self
local aTable = {}
aTable._index = {}

function aTable.initialize(MinSpeed, MaxSpeed, AccelIncrement)
	local self = {}
	setmetatable(self, aTable)

	-- Values --
	self.SetValues = {}
	self.SetValues.MIN_SPEED = MinSpeed
	self.SetValues.MAX_SPEED = MaxSpeed
	self.SetValues.ACCEL_INCREMENT = AccelIncrement
	self.SetValues.GRAV = workspace.Gravity
	--
	print(self)
	aTable = self
end
print(aTable)
function aTable:start(self2)
	print(self.SetValues)
	foo()
end
aTable.initialize()
print(aTable)

this probably breaks something though