Will task.spawn fix this?

local part = Instance.new("Part")
part.Parent = workspace

task.spawn(function()
wait(1)
part:Destroy()
end)

script:Destroy()

Normally, the part would wait a second, then destroy itself, and then the script would be destroyed.
But, with task.spawn, the script will be destroyed immediately.
My question is:

Would the part be destroyed? On the one hand, the script is technically destroyed before it can execute that (because of the task.spawn), but on the other hand maybe task.spawn guarantees that function will be run no matter what, since it’s kind of separate?

What exactly are you trying to achieve?

Why can’t you just answer my question tho

I’m tryna use instancing to make a body velocity for knockback
but the script is part of the projectile
and i want the projectile to destroy itself when it hits
but there needs to be some delay on the body velocity before its destroyed
but obviously since the script, which is part of the projectile, will be destroyed
so i cant destroy it in there
and i was hoping task.spawn might help my issue

If you want some delay before the body velocity gets destroy, you can use

game.Debris:AddItem(object, delay time)

oh my god you legend this might have been one of the most valuable things ive learned in a while

1 Like

Debris uses the outdated delay() global so it’d be better to use.

task.delay(n, function()
	instance:Destroy()
end)

Where “n” is some length of time.