What do you want to achieve? I want the instance to get disconnected from a function when it’s removed.
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.
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.
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()
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