Instance not getting disconnected from function?

  1. What do you want to achieve? I want the instance to get disconnected from a function when it’s removed.

  2. What is the issue? Apparently when destroying the instance it gets disconnected from all connections, however, i’ve added a print to check if it’s still there, and it keeps printing even after i destroy the instance.

  3. What solutions have you tried so far? I did look for solutions on the Developer Hub, and the forums too, but i can’t seem to find one at all, i tried using Disconnect() but it just disables the entire script for the rest of instances.

I’m making a Zombie, i’m using one script to handle all of them, however as you can see, the problems i stated above are happening right now and i’ve tried many solutions but i just can’t solve it, maybe i’m skipping something, perhaps i just don’t have enough knowledge, here’s the script that i’m trying to solve (Sorry if it’s a disaster):

local child = folder.ChildAdded

child:Connect(AI)

On line 41 (the Testing! print), it keeps printing after i destroy the Zombie, which i think means it didn’t get disconnected, this just stacks as Zombies keep getting added into the AI Folder, which is where they’re placed, hope someone can help, and thanks in advance.

If you see any other problems on the script please point them out so i can try to fix them too…

As the script is not in the AI itself, the loop does not break when you destroy the character (?). You may want to add an extra while condition that checks if the agent is valid or a descendant of the workspace.

1 Like

I added while agent:IsDescendantOf(folder) do before the loop, but the same thing keeps happening, not sure if that’s what you meant.

I know you said you tried disconnect, but I just wanted to make sure you did it correctly. When you :connect(), it should return a connection object. Make sure you :disconnect() that object and not something else.

local ai1conn = child:connect(AI)
-- later...
ai1conn:disconnect()
local child = folder.ChildAdded

local conn;
conn = child:Connect(AI)

folder.ChildRemoved:Connect(function()
	conn:Disconnect()
end)

I added this, however it disables the entire script from working and the printing still continues, i think i’m just doing it wrong.

So, you only have one connection. Disconnecting it will stop all of them.

You need a way to kill the the specific connection and not all of them.

I’m currently in class, but basically what you want is something like this:

folder.ChildAdded:connect(function(obj)
    startAI(obj)
end)
folder.ChildRemoved:connect(function(obj)
    endAI(obj)
end)

startAI will add the obj to some table as the index and a true/false value as the value.

endAI will set AIs[obj] = false which will then make the while stop.

So really the best way is to have one while loop at the very end of the script that loops through a table of all active AIs rather than having a while loop for every AI.

local allAIs = {}

function addAI(obj)
    allAIs[obj] = {some props if needed}
end

function removeAI(obj)
    allAIs[obj] = nil
end

-- at very end of script
while (true) do
    wait() -- don't forget this
    for agent, properties in pairs(allAIs) do
        -- do stuff
    end
end
1 Like