Connections[TaggedPart][i] = func(TaggedPart) -- here i store the connection in a table
-- this function disconect the conection in case it is no longer a killpart
CollectionService:GetInstanceRemovedSignal(i):Connect(function(TaggedPart)
Connections[TaggedPart][i]:disconnect()
Connections[TaggedPart][i]=nil
end)
local CollectionService = game:GetService("CollectionService")
local Connections = {}
local Part = Instance.new("Part")
Part.Parent = game.Workspace
CollectionService:AddTag(Part,"Kill")
wait(1)
local func = require(script.Kill)
local TaggedParts = CollectionService:GetTagged("Kill")
for _, TaggedPart in pairs(TaggedParts) do
if Connections[TaggedPart] == nil then
Connections[TaggedPart] = {}
end
Connections[TaggedPart] = func(TaggedPart)
end
CollectionService:GetInstanceAddedSignal("Kill"):Connect(function(TaggedPart)
if Connections[TaggedPart] == nil then
Connections[TaggedPart] = {}
end
Connections[TaggedPart] = func(TaggedPart)
end)
CollectionService:GetInstanceRemovedSignal("Kill"):Connect(function(TaggedPart)
Connections[TaggedPart]:disconnect()
Connections[TaggedPart]=nil
end)
local Part = Instance.new("Part")
Part.Parent = game.Workspace
CollectionService:AddTag(Part,"Kill")
Maybe youâd have a better, much easier time defining whether it can be active or not by using an external value such as an attribute, or just a boolean instead, so you can re-instantiate it instead of calling to a new connection event, which would just overall take up more memory.
Wouldnât a better way of going about this just be by using a value thatâs stored in the parts address instead? Simply put, a boolean you can contain several other values so you donât have to re-define the connection.
Iâm not sure how much a .Touched connection takes up in general (regarding memory), but maybe itâd be a more efficient way to just use a boolean externally instead, so you donât have to ever call to disconnect, or call to re-connect?
local CollectionService = game:GetService("CollectionService")
local Connections = {}
local Part = Instance.new("Part")
Part.Parent = game.Workspace
CollectionService:AddTag(Part,"Kill")
wait(1)
local func = require(script.Kill)
local TaggedParts = CollectionService:GetTagged("Kill")
for _, TaggedPart in pairs(TaggedParts) do
if Connections[TaggedPart] == nil then
Connections[TaggedPart] = {}
end
Connections[TaggedPart] = func(TaggedPart)
end
CollectionService:GetInstanceAddedSignal("Kill"):Connect(function(TaggedPart)
if Connections[TaggedPart] == nil then
Connections[TaggedPart] = {}
end
Connections[TaggedPart] = func(TaggedPart)
end)
CollectionService:GetInstanceRemovedSignal("Kill"):Connect(function(TaggedPart)
Connections[TaggedPart]:disconnect()
Connections[TaggedPart]=nil
end)
local Part = Instance.new("Part")
Part.Parent = game.Workspace
CollectionService:AddTag(Part,"Kill")