what is the difference between them?
task.delay is like delay that delay the function to start and delay(0) that same like spawn function but spawnfunction may faster, use task.spawn instead than spawn
task.delay will delay the function inside by the set amount of seconds without delaying the code. task.wait delays the part of the script it is set inside of.
task.delay does not yield code, while also spawning the function after n seconds after the current heartbeat
task.delay(2, function()
print("B")
end)
print("A")
--Output:
-- A
--2 seconds later
-- B
task.wait(2)
print("B")
print("A")
--Output:
-- waits 2 seconds
-- B
-- A
What are the benefits of using this over coroutines?
Assuming you mean task.spawn instead of task.delay, with the coroutine library there is an issue with RemoteFunctions (and possibly bindables but not 100% sure) and possibly other callback functions where if you’re yielding a thread through coroutine.yield(coroutine.running()) and later resuming it, it still hangs infinitely, particularly with custom events and yields. task.spawn(thread)
fixes it. It’s also an all-in-one solution to spawning new threads and resuming paused threads. With the coroutine library, you need to create a coroutine (through means of coroutine.create or wrap) and then call/resume it (wrap(f)() or resume(create(f))). Instead, task.spawn creates it and resumes it instantly.
task.delay is not a replacement for coroutine.create/wrap but it does still have its uses. For example it replaces debris which apparently runs at 30hz (same frequency as wait()) instead of the monitor’s refresh rate.