Scheduling code is useful in many situations, such as ensuring code executes after a specific action or cycle has completed, or delaying code for a specific duration of time. You can use the task library to optimize Roblox's Task Scheduler to manage and schedule code. You can also use a similar library called coroutine to schedule code which has some additional functionality.
The following table lists the relevant legacy global methods and their preferred, more optimized counterparts:
|Legacy Global Methods||Task Methods||Additional Alternative Methods|
|delay(n, f)||task.delay(n, f)|
|spawn(function () f(uv1, ...) end)||task.defer(f, uv1, ...)||task.delay(0, f, uv1, ...)|
|delay(n, function () f(uv1, ...) end)||task.delay(n, f, uv1, ...)|
task.spawn() takes a thread or function and resumes it immediately through the engine’s scheduler. Additional arguments are passed to the thread or function being resumed.
The following code sample is an example of how you can use task.spawn() when calling a function that may yield while iterating over a set of objects:
1local function playerAdded(player)2 ...3 (yield)4end56for _, player in ipairs(Players:GetPlayers()) do7 task.spawn(playerAdded, player)8end9
task.defer() takes a thread or function and defers it until the next resumption cycle at which point it is resumed with the engine’s scheduler. Additional arguments are passed to the thread or function resuming.
You should typically use this when you want similar behavior to task.spawn() but don’t care about the thread running immediately. The following code sample illustrates how the print statement for "A" will defer until after the print statement for "B" executes:
1task.defer(print, "A")2print("B")3--> B4--> A5
task.delay() takes a thread or function and schedules it for resumption after the given amount of time elapses on the next Heartbeat step. The thread resumes with built-in error handling and support for other engine features. Any additional arguments are passed to the thread or function resuming.
Since the actual delay time may vary, the following code sample illustrates how you can calculate it by passing the current time as an argument:
1task.delay(2, function (scheduledTime)2 print(os.clock() - scheduledTime) --> 2.0387023end, os.clock())4
A duration of zero will result in the thread or function resuming on the next step.
The actual yield time may vary. The following code sample illustrates how this method returns it for convenience:
Since the actual delay time may vary, the following code sample illustrates how you can get the actual time by storing the method's return value:
1local elapsedTime = task.wait(2) -- wait for 2 seconds2print(elapsedTime) --> 2.07929413