I'm getting "thread is not yieldable", what's causing it?

I’m getting a very strange error stating that I cannot yield in a thread, but why is that? The error is pointing to task.wait() and I cannot figure out what’s wrong with it.

RobloxStudioBeta_bIFcxUPg1i

RobloxStudioBeta_4pPksv5mKC

function Server:Get(name: string): any -- Yields
	local calls = 0
	
	-- Wait for data to be created
	repeat
		task.wait()
		
		-- Prevent infinite yield
		calls += 1
		if calls > 100000 then
			error("Tried to retrieve a stream but reached the wait limit, did you forget to create the stream?")
		end
	until Streams[name].DataWithInterface ~= nil
	
	-- Return the data
	return Streams[name].DataWithInterface
end

Use task.spawn instead.

Why would I use task.spawn() though?, it doesn’t wait and thus it doesn’t achieve my goal. My method is suppose to yield until data is available.

All other solutions to your situation use a new thread. Make a function non-yielding? - #4 by VeinTweek

Although your fix was correct, you gave me little information regarding the implementation of it. Someone else told me that metatables cannot yield at any point, which is what happened in my case, I just simply had to wrap a call inside a metatable in a task.spawn() and that fixed the issue.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.