"attempt to call a nil value" resuming the main thread with coroutines

local c = coroutine.running() spawn(function() coroutine.resume(c) end) wait(1) print("hello")

Running this in the command line errors. Replacing wait(1) with coroutine.yield() also errors, but replacing it with pcall(coroutine.yield) does not.


I love mysterious things like this.

Maybe this is the C-side task scheduler’s equivalent of coroutine.resume’s “attempt to resume a dead coroutine” error?

Why it doesn’t error with pcall is very simple:pcall(coroutine.yield) creates a new (non-ROBLOX) thread (different from c) which you yield, so it’s basically something that’s yielded forever. Both wait() and yield() put the thread in the task scheduler and say “resume me then”, but spawn() always comes first, so spawn() does the scheduler’s job, and the scheduler then tries it and is mad. pcall(yield) yields forever so nobody stole the sched’s job. (hence it still prints hello) I just ran more code, and even if you manually resume the pcall-thread (and end it), the call to pcall never ends… that’s extra weird.

1 Like