zakeIlo
(zakello)
November 1, 2024, 4:54am
#1
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
Exozorcus
(Exozorcus)
November 1, 2024, 5:08am
#2
are you running start before initialize?
zakeIlo
(zakello)
November 1, 2024, 5:09am
#3
No, I initialize first, then run table:start
.
Exozorcus
(Exozorcus)
November 1, 2024, 5:10am
#4
isnt table a metatable?
like table.insert
roblox might not be very happy with changing that
zakeIlo
(zakello)
November 1, 2024, 5:11am
#5
Yes, the table isnt actually called that, I just changed the name when i was posting it on the devforum, ill edit it
zakeIlo
(zakello)
November 1, 2024, 5:16am
#6
If I print “self” in aTable:start
, it just prints both function names, nowhere is there the table I returned with the “initialize” function
Exozorcus
(Exozorcus)
November 1, 2024, 5:20am
#7
this is pretty weird
im gonna try do some testing
zakeIlo
(zakello)
November 1, 2024, 5:22am
#8
Alright, much appreciated
minimum
Exozorcus
(Exozorcus)
November 1, 2024, 5:28am
#9
ok this probably has something to do with script context
bleintant
(bleintant)
November 1, 2024, 5:32am
#10
Where are you calling the start method?
bleintant
(bleintant)
November 1, 2024, 5:35am
#12
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)
zakeIlo
(zakello)
November 1, 2024, 5:35am
#13
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)
Exozorcus
(Exozorcus)
November 1, 2024, 5:36am
#14
[“_index”] = “*** cycle table reference detected ***”,
roblox not happy
zakeIlo
(zakello)
November 1, 2024, 5:38am
#15
still returning nil, in the initialize function, self
prints just fine, but in the other one it acts like theres no table
bleintant
(bleintant)
November 1, 2024, 5:39am
#16
Are you getting any errors, the code you had originally works for me in studio?
Exozorcus
(Exozorcus)
November 1, 2024, 5:40am
#17
oh i fixed it on accident lol
min characters
zakeIlo
(zakello)
November 1, 2024, 5:40am
#18
nope, theres no errors, I dont know what the issue is
Exozorcus
(Exozorcus)
November 1, 2024, 5:41am
#20
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