Task

Roblox has updated its best practices for handling asynchronous tasks with the introduction of the task library, which provides a cleaner and more efficient way to handle background tasks than the older spawn, delay, and wait functions. The task library includes methods like task.spawn, task.delay, and task.wait, which are designed to be more reliable and perform better.
OperationDetails
task.spawntask.spawn is used to asynchronously run a function without yielding the caller.
task.delaytask.delay is used to execute a function after a specified delay, allowing the creation of timed or delayed actions without blocking other operations.
task.waittask.wait is used to pause the current task for a specified duration.

task.spawn

local function doBackgroundTask()
    print("Background task started")
    for i = 1, 10 do
        print("Processing step " .. i)
        wait(1)  -- Simulate some processing delay
    end
    print("Background task completed")
end

print("Main script execution")
task.spawn(doBackgroundTask)
print("Main script continues immediately after launching background task")
Image 1
Roblox Studio

task.delay

local function delayedTask()
    print("Delayed task executed after 5 seconds")
end

print("Main script execution")
task.delay(5, delayedTask)  -- Delay execution of delayedTask by 5 seconds
print("Main script continues immediately after scheduling delayed task")
Image 1
Roblox Studio

task.wait

local function waitForSomeTime()
    print("Wait starting")
    task.wait(5)  -- Pauses this function, not the entire script, for 5 seconds
    print("Wait ended after 5 seconds")
end

print("Main script execution")
task.spawn(waitForSomeTime)
print("Main script continues immediately, while waitForSomeTime pauses internally")
Image 1
Roblox Studio