Old Roblox tutorials still use wait(), spawn() and delay(). They work, but they run on the legacy scheduler and can throttle or drift. The task library is the current replacement.
The swap
-- old
wait(1)
spawn(function() print("hi") end)
delay(2, function() print("later") end)
-- current
task.wait(1)
task.spawn(function() print("hi") end)
task.delay(2, function() print("later") end)
Why it matters
-
task.wait()resumes on the next engine step and returns the real time waited. Plainwait()can be throttled when the game is busy. -
task.spawnruns the function immediately until it yields.spawnwaits a frame first, which causes ordering bugs. -
task.deferruns the function at the end of the current step. Use it when you want "after this code finishes, but this frame". -
task.cancel(thread)stops a thread you started withtask.delayortask.spawn. The old functions give you no way to do that.
A small pattern: cancellable timeout
local timeout = task.delay(5, function()
warn("Request took too long")
end)
local ok, result = pcall(function()
return MyRemoteFunction:InvokeServer()
end)
if ok then
task.cancel(timeout)
end
If the call returns in time, the warning never fires.
Quick rules
- Use
task.waitin every loop. Never use a barewhile true do endwithout a wait. - Use
task.spawnfor work that should start now. Usetask.deferwhen order matters. - Keep the thread handle if you may need to cancel it.
Related
I keep a starter kit of safe, server-validated Roblox scripts (datastore, shop, leaderboard) here: Roblox Game Starter Kit. There is a launch code LAUNCH25 for 25% off until Dec 17. The tutorial above is free either way.
Top comments (0)