Stop A Thread When A Part Is Deleted

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to be able to stop a thread from outside of the thread.

  2. What is the issue? Include screenshots / videos if possible!
    I have one script using collection service to control multiple parts. It creates threads for each of them, and I want the threads to be deleted when the parts get deleted.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked at other posts on the developer forum, but I didn’t find any solutions.
    I thought of checking in between every line if the part is deleted, but that would be annoying to code and could cause lag.
    I also thought of creating a new thread by cloning a script and then deleting it to stop it, but I’m not sure if that would work or if it would cause other problems.
    Is there a better way to do this?

1 Like

Join the club :slight_smile:


On a more serious note, using Instance.AncestoryChanged and then checking if the new parent is nil works pretty well. It’s different than the object actually being “destroyed” but it’s usually about the same thing.

Are you using coroutines or roblox events/connections? If you’re using connections, just store them in a table then loop though that table disconnecting them when the parent changes to nil.

Let me know if you’ve got any questions/or if this doesn’t work for you because Roblox still hasn’t added a Destroyed event like Unity :+1:


Using events of the Part also works. Note this might not be possible in all cases though, like when you need RunService events.

Parent the connection to the part and when the part gets destroyed the connection dies too

workspace.DescendantRemoving:Connect(function(descendant)
	print(descendant.Name.." was removed!")
	--do other code if necessary
	script:Destroy()
end)

This will destroy the current script if a descendant is removed (destroyed or parented elsewhere).

workspace.DescendantRemoving:Connect(function(descendant)
	print(descendant.Name.." was removed!")
	--do other code if necessary
	script.Disabled = true
end)

This is essentially the same but will only disable the script, not delete it.

I making a thread that runs code for each part. I want the code to stop when the part is deleted so that it doesn’t print any errors for the part being gone. There might also be other problems caused by the part being gone, but the thread still running, but I’m not sure.
I could use Instance.AncestoryChanged to check if the part is deleted, but once it is deleted how do I stop the thread?
Looping through a table with the connections to disconnect them would work, but I also have other code running that I need to stop.
I’m using spawn() to create a thread, but I could change it to coroutines if it is easier to stop.

You’d need to check if the part still exists after each yield, then end that thread if it does. Code that runs all in a line without any yielding functions runs instantly*, so you don’t need to worry about the part being destroyed during that interval.
* It’s not really “instant”, but the engine basically breaks everything up in cycles, so unless you delete the part within that code or if the code yields (goes onto the next cycle), then the part will still be there. Note I’m not an expert on that.

It would be helpful if you sent some code so I could see what you’re trying to do :+1:


Also note that workspace.DescendantRemoving also just checks if the instance was parented to nil after it’s ancestry is changed, so if you’re looking to do this with a specific part using code like this would be better:

part.AncestryChanged:Connect(function(changedInstance, parent)
    if not parent then
        --Code
    end
end)