Is there a way to keep code running on an event where the object is destroyed?

Hey, so like in this example:

--//Script is not stored in the object directly
object.Touched:Connect(function()
object:Destroy() --//In my scenario I would like to destroy the object before continuing
--//do some stuff
end)

Is there a way I could get it so like, the function will continue running despite destroying the object?
Not sure if this is possible or not…thanks.

2 Likes

I’m pretty sure that the code will keep running even though the first thing it does is destroy an object. The code will still run, but the Touched event will no longer fire since the object is destroyed.

4 Likes

After testing this again, the code definitely stops executing after the object is destroyed…which is weird :confused:

That definitely makes sense. When you destroy an instance the connections are disconnected. Just destroy at the end.

1 Like

You could try to mimic the part being destroyed by setting its Transparency and CanCollide then at the end :Destroy() it. Though I would think the code would still execute ¯_(ツ)_/¯

1 Like

maybe something like this?

object.Touched:Connect(part)
    spawn(function()
        --code here, this will run separately
    end)
    object:Destroy()
end)
1 Like