I’m making a component based on CollectionService and this is what I done so far!, How do I improve it?
local CSSComponent = {}
CSSComponent.__index = CSSComponent
CSSComponent.ClassName = "CSSComponent"
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local Packages = ReplicatedStorage.Packages
local Signal = require(Packages.Signal)
function CSSComponent.new()
local Tags = {
["UsingMove"] = {};
["Stunned"] = {};
["Ragdoll"] = {};
["Immunity"] = {};
};
setmetatable(Tags, CSSComponent)
return Tags
end
function CSSComponent:AddTag(List, Tag)
if not self[Tag] then self[Tag] = {} end
local relatedTag = self[Tag]
for _, obj in pairs(List) do
CollectionService:AddTag(obj, Tag)
table.insert(relatedTag, obj)
end
end
function CSSComponent:RemoveTag(List, Tag)
if not self[Tag] then self[Tag] = {} end
local relatedTag = self[Tag]
for _, obj in pairs(List) do
if CollectionService:HasTag(obj, Tag) then
CollectionService:RemoveTag(obj, Tag)
for number, instance in pairs(relatedTag) do
if instance.Name == obj.Name then
table.remove(relatedTag, number)
end
end
end
end
end
function CSSComponent:CheckTag(List, Tag)
if not self[Tag] then self[Tag] = {} end
local relatedTag = self[Tag]
local checks = {}
for _, obj in pairs(List) do
if CollectionService:HasTag(obj, Tag) then
checks[obj] = true
else
checks[obj] = false
end
end
return checks
end
function CSSComponent:TagChanged(List, Tag)
local newList = {}
for _, obj in pairs(List) do
local AddedSignal, RemovedSignal = Signal.new(), Signal.new()
local addConnection
addConnection = CollectionService:GetInstanceAddedSignal(Tag):Connect(function(v)
AddedSignal:Fire(obj)
end)
local removeConnection
removeConnection = CollectionService:GetInstanceRemovedSignal(Tag):Connect(function(v)
RemovedSignal:Fire(obj)
end)
newList[obj] = {
AddSignal = AddedSignal;
RemoveSignal = RemovedSignal;
AddConnection = addConnection;
RemoveConnection = removeConnection
};
end
return newList
end
return CSSComponent