[13:36:28.877 - ServerStorage.Objects.Purse:32: attempt to call a nil value]
But when I debug that line of code the object in question does not to me look nil so I’d just like some pointers please on how to debug this. I’m really new to Roblox Studio and lua.
function List:Count()
if self == nil then
return 0
end
return self._count
end
I’ve changed my code to this (just to help debugging):
And I was expecting the code to return on line 36 maybe because PipList was nil, but now I get the error on line 39:
[13:45:47.357 - ServerStorage.Objects.Purse:39: attempt to call a nil value]
I’m really stumped because self isn’t nil, PipList isn’t nil so what is the issue? I’m sure this is something super obvious I’m missing, any pointers greatly received, thank you for reading…
-- https://devforum.roblox.com/t/all-about-object-oriented-programming/8585
local List = {}
List.__index = List
function List.new()
local self = {
-- Define members of the instance here, even if they're `nil` by default.
_list = {},
_count = 0,
}
-- Tell Lua to fall back to looking in List.__index for missing fields.
setmetatable(self, List)
return self
end
function List:GetList()
return self._list
end
function List:Add(item)
self._count = self._count + 1
self._list[self._count] = item
end
function List:Get(index)
return self._list[index]
end
function List:RemoveAt(index)
if index > self._count then
error("Element does not exist index:"..index..". Current count:"..self._count)
end
-- Just move item at the end into position of removed item...
self._list[index] = self._list[self._count]
-- and then end position null
self._list[self._count] = nil
self:RemoveAllNulls()
end
function List:RemoveAllNulls()
local newList = {}
local newListIndex = 1
for i, item in pairs(self._list) do
if item ~= nil then
newList[newListIndex] = item
newListIndex = newListIndex + 1
end
end
self._list = newList
self._count = newListIndex - 1
end
function List:Count()
if self == nil then
return 0
end
return self._count
end
return List
The error says that the “Count” method is nil, but since __index references List, I don’t know how that could be. Where are you creating PipList and the other things in that script?
So I guess that’s where it gets a bit complicated to show but here’s the important bit I think (see HERE IS WHERE THE PIPLIST IS CREATED). PipList, is, and I think this might be the problem a ‘property’ of another object:
local Purse = {}
-- Removed code for brevity...
local List = require(game.ServerStorage.Global.List)
Purse.__index = Purse
function Purse.new()
local self = {
-- Define members of the instance here, even if they're `nil` by default.
-- Removed code for brevity...
-- HERE IS WHERE THE PIPLIST IS CREATED:
PipList = List.new(),
-- Removed code for brevity...
}
-- Tell Lua to fall back to looking in Purse.__index for missing fields.
setmetatable(self, Purse)
return self
end
function Purse:GetTotalCount()
if self == nil then
return 0
end
if self.PipList == nil then
return 0
end
if self.PipList:Count() == nil then
return 0
end
return self.PipList:Count()
end
-- Removed code for brevity...
return Purse
Thanks @cjjdawg turns out you’re exactly correct I was setting another List earlier in my code. The variable I’m replacing PipList with is another instance of a List so to my mind, and coming from .NET I would expect that to work fine. Not real code but it’s basically something along these lines:
purse = Purse.new
local activePipList = loadActivePipFromDB()
purse.PipList = activePipList
I guess this is a limitation with my understanding of what’s going on with this code I think, in my OOP implementation, maybe:
List.__index = List
Anyway thanks again for your time, I think I’ll probably just take the contents of the list and put them into the existing list rather than try and change the actual underlying reference.