Destroying object on touch - signal ordering issues

I would like to understand the following scenario. Two parts are colliding with each other. One was destroying itself and the other had some logic based on the touchedPart attributes. The code was crashing because the script was accessing the parent of the destroyed part which nil due to the destruction.

I fixed this issue by the DebrisService, with a delay of 0, which I understand delegates the :Destroy() call. While this does seem to solve the problem, I am uncertain if there are any side-effects? Is this the right solution? Is my understanding accurate?

During testing, I also tried using :ClearAllChildren() which would only trigger the OnTouched of the object which destroys itself but not the other OnTouched function, which was still not what I expected.

Attachment Baseplate.rbxl (22.3 KB)

Maybe try adding some checks to see if the parent actually exists
:Destroy() I believe sets the parent to nil (removing it from workspace or wherever)

if part.Parent ~= nil then
   -- do stuff

fun fact you can actually set a script’s parent to nil

Thanks. I did think about that but I was wondering if I could even avoid it in the first place.

1 Like

If your issue has been solved make sure to mark @kingerman88 post as the solution!

It has not been resolved at all.