Yeah, honestly, it’s probably not even worth fixing. Again, once he does fix it, he’ll most likely have event issues regarding whether it connects or not in the future, so honestly he should probably just abandon this and hop on a better solution!
But, that’s just my take. I’m not saying he should, but it’s probably a better alternative altogether to just use a better, more efficient method.
So, from what I understand, you’re essentially trying to make a kill brick?
Why not instantiate a region3 for each kill brick, have each kill brick non-can collide, and if anything steps into it at any given time, execute the code you’re trying to, (aka kill them?)
The instance of which has been touched? Or the connection itself? I’ve been reviewing some of your code, and from what I’m seeing, it kind of looks like you’re trying to return the connection event as a whole. I’m not entirely sure if you’re going to get any expected results by returning just the connection event.
Maybe instead, return the character inside the .Touched connection, then that way, when you return the connection, it’ll maybe return that character as well?
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")