task

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่


ข้อมูลสรุป

ฟังก์ชัน
spawn(functionOrThread: function | coroutine,...: Variant):coroutine
defer(functionOrThread: function | coroutine,...: Variant):coroutine
delay(duration: number,functionOrThread: function | coroutine,...: Variant):coroutine
wait(duration: number):number
cancel(thread: coroutine):()

เอกสารอ้างอิงเกี่ยวกับ API
ฟังก์ชัน
spawn
task.spawn(
functionOrThread:function | coroutine, ...:Variant
พารามิเตอร์
functionOrThread:function | coroutine
...:Variant
ส่งค่ากลับ
ตัวอย่างโค้ด
การสร้างงาน
local function greet(name, punctuation)
print("สวัสดี, " .. name .. punctuation)
end
print("ก่อน") --> ก่อน
task.spawn(greet, "Roblox", "!") --> สวัสดี, Roblox!
print("หลัง") --> หลัง
-- คอรูทีนที่ถูกระงับสามารถถูกเรียกคืนด้วยอาร์กิวเมนต์ได้เช่นกัน.
local worker = coroutine.create(function(label)
print("คนงานเริ่มต้นด้วย", label)
end)
task.spawn(worker, "งาน 1") --> คนงานเริ่มต้นด้วย งาน 1
print(coroutine.status(worker)) --> ตาย

defer
task.defer(
functionOrThread:function | coroutine, ...:Variant
พารามิเตอร์
functionOrThread:function | coroutine
...:Variant
ส่งค่ากลับ
ตัวอย่างโค้ด
การเลื่อนงาน
print("หนึ่ง") --> หนึ่ง
task.defer(function(message)
print(message) --> เลื่อน
end, "เลื่อน")
task.spawn(function()
print("สอง") --> สอง
end)
print("สาม") --> สาม
-- ฟังก์ชันที่เลื่อนจะทำงานหลังจากที่โค้ดปัจจุบันเสร็จสิ้น ดังนั้นมันจึงพิมพ์สุดท้าย.

delay
task.delay(
duration:number, functionOrThread:function | coroutine, ...:Variant
พารามิเตอร์
duration:number
functionOrThread:function | coroutine
...:Variant
ส่งค่ากลับ
ตัวอย่างโค้ด
การหน่วงเวลา
local scheduledAt = os.clock()
task.delay(1, function(message)
local elapsed = os.clock() - scheduledAt
print(message, elapsed >= 1) --> สวัสดีที่หน่วงเวลา true
end, "สวัสดีที่หน่วงเวลา")
print("กำหนดเวลาแล้ว") --> กำหนดเวลาแล้ว
-- เธรดที่ส่งคืนสามารถถูกยกเลิกได้ก่อนที่จะทำงาน
local cancelled = task.delay(2, function()
print("ไม่เคยพิมพ์")
end)
task.cancel(cancelled)

desynchronize
task.desynchronize():()
ส่งค่ากลับ
()
ตัวอย่างโค้ด
การทำให้ไม่ซิงโครไนซ์
-- ภายในสคริปต์ที่เป็นลูกหลานของ Actor, desynchronize() จะทำให้เธรด
-- ดำเนินการในเฟสขนานถัดไป ซึ่งงานหนักสามารถทำงานควบคู่ไปกับ Actor อื่น ๆ
-- นอกเหนือจาก Actor จะเกิดข้อผิดพลาด ซึ่ง pcall ช่วยให้เราสังเกตได้
local ok = pcall(task.desynchronize)
print(ok) --> false (เมื่อสคริปต์นี้ไม่ใช่ลูกหลานของ Actor)

synchronize
task.synchronize():()
ส่งค่ากลับ
()
ตัวอย่างโค้ด
การซิงโครไนซ์งาน
-- ภายในสคริปต์ที่เป็นลูกหลานของ Actor, synchronize() จะทำให้เธรด
-- กลับมาที่เฟสแบบอนุกรมถัดไป ซึ่งปลอดภัยในการแก้ไขโมเดลข้อมูล
-- นอกเหนือจาก Actor จะเกิดข้อผิดพลาด ซึ่ง pcall ช่วยให้เราสังเกตได้
local ok = pcall(task.synchronize)
print(ok) --> false (เมื่อสคริปต์นี้ไม่ใช่ลูกหลานของ Actor)

wait
task.wait(duration:number):number
พารามิเตอร์
duration:number
ค่าเริ่มต้น: 0
ส่งค่ากลับ
ตัวอย่างโค้ด
รอภารกิจ
local elapsed = task.wait(1)
print(elapsed >= 1) --> true
-- หากไม่มีอาร์กิวเมนต์ เธรดจะกลับมาทำงานใน Heartbeat ถัดไป.
local frameTime = task.wait()
print(frameTime < 1) --> true

cancel
task.cancel(thread:coroutine):()
พารามิเตอร์
thread:coroutine
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ยกเลิกงาน
local thread = task.delay(5, function()
print("สวัสดีชาวโลก!") -- ไม่เคยพิมพ์
end)
task.cancel(thread)
print(coroutine.status(thread)) --> dead
-- การยกเลิกเธรดที่กำลังทำงานอยู่ในขณะนี้ไม่อนุญาตและจะเกิดข้อผิดพลาด
local ok = pcall(task.cancel, coroutine.running())
print(ok) --> false

©2026 Roblox Corporation. Roblox, โลโก้ Roblox และ Powering Imagination เป็นส่วนหนึ่งของเครื่องหมายการค้าที่จดทะเบียน และไม่ได้จดทะเบียนของเราในสหรัฐฯ และประเทศอื่นๆ