I found this on Roblox Debris api and I wondered why there is 3 and fuction inside the bracket after delay. I also wonder what “prevent the current one from yielding” mean
delay(3, function()
if projectile and projectile.Parent then
projectile:Destroy()
end
end)
This solves the above issues, as it spawns a new thread to prevent the current one from yielding and checks to see if it can be destroyed. However at this point a simple command has already become quite complicated and an unnecessary thread is being created.
The idea behind delay is sort of like “after x seconds, do this”. It has been replaced with task.delay (same with how wait was replaced with task.wait).
Snippet from the wiki article on the task library.
The first parameter, 3 in this case, is just how long you want to yield before running the function. The second argument, as stated by the wiki screenshot above, can be a thread (returned from a coroutine) or a function. Here’s two examples:
function doSomething()
-- your code here
end
task.delay(5, doSomething) -- would wait 5 seconds, then on the next Heartbeat `doSomething` would be called.
Or, you can just make the function on the spot like so:
task.delay(5, function()
-- your code here
end)
Both would work just fine. As for the use case of task.delay… I’m not sure. I have never seen a problem in my code where I said “I’ll use that new task.delay function.”
A use for task.delay would be for cooldown/debounce. You could wrap a function changing the cooldown/debounce value with task.delay, so that you don’t have to change it at the end of the function.