Task
Task
is a utility for creating non-blocking, coroutine-based tasks that can run concurrently with the main update loop.
It allows pausing, resuming, and killing coroutines safely without blocking the UI or main loop.
Task Utility
This module does not USE
sys.Task()
function which comes with LuaRT.
Constructors
Task.new()
Function
Task.new(fn)
- Creates a new non-blocking task.
Parameters:
fn
function- The function to be executed as a task.
Returns:
Task
Task- The created task.
Functions
Task.wait()
Function
Task.wait(duration)
- Pauses execution inside a task for the specified duration.
Can only be called inside a task.
Parameters:
duration
number- The duration to wait in seconds.
Methods Object
(Task):Kill()
Method
- Stops the task immediately.
(Task):Pause()
Method
- Pauses the task without resetting its coroutine.
(Task):Resume()
Method
- Resumes a paused task.
Notes
- Tasks are automatically updated every frame via
UpdateService.Heartbeat
. - Do not call
Task.wait
outside a task; it relies on the heartbeat delta-time for timing. - Task errors are logged via
warn
and stop the task automatically.
Example
--- Create a task that prints "Task running every second..." every second
--- It does not block the main update loop
local myTask = Task.new(function()
while true do
Task.wait(1)
print("Task running every second...")
end
end)
--- Pause, resume, and kill the task
myTask:Pause()
myTask:Resume()
myTask:Kill()