You can write your topic however you want, but you need to answer these questions:
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.
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.
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?
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
Using events of the Part also works. Note this might not be possible in all cases though, like when you need RunService events.
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
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)