Hello everyone, I am working on a system and using something called “components” basically it is a modular class that runs on objects tagged with CollectionService and just wondering what is going on here.
When a new item is tagged it gets added to a table for later reference and calls its functions
local function component_added(item)
local c = module.new(item)
babies[item] = c
end
local function component_removed(item)
local babyModule = babies[item]
if babyModule ~= nil then
if babyModule.Clean ~= nil then
babyModule:Clean()
end
babies[item] = nil
end
end
The system works great the first time, but the second time it doesn’t and I have found the issue, in this function above, the “item” is nil.
These functions are just connected to their relative “GetInstance___Signal” event.
The important component functions information looks like so:
function Buildable.new(instance: Instance)
local self = setmetatable({
["_initialized"] = false,
["Instance"] = instance,
["Maid"] = loader.Maid(),
}, Buildable)
self:_init()
return self
end
function Buildable:_init()
...
self.Maid:GiveTask(self.Instance:GetAttributeChangedSignal("Attack"):Connect(function()
CollectionService:RemoveTag(self.Instance, "Buildable")
end))
end
And the clean up function:
function Buildable:Clean()
print("Cleaning Buildable")
self.Maid:DoCleaning()
...
self.Instance:SetAttribute("Attack", false)
CollectionService:AddTag(self.Instance, "Buildable")
end