Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I use coroutine.yield()?

I am wondering how to use coroutine.yield() because i can't figure out how it works. I have tried to understand the article on the Roblox's own devhub, but I just can't understand it. Can anyone help?

Code:

function Alarms.On(Color)
	for i, v in pairs(Alarms.AlarmTable) do
		v.Activate(Color) -- Activates a bunch of smaller scripts
		coroutine.yield() -- Tries to yield but fails
	end
end

1 answer

Basically coroutine.yield() will pause the script so nothing continues until what is inside of the () is completed. You appear to be using it wrong in your script, an example of how I would use it is below.

local num = 0;

while true do
	wait();
	print("Adios: " .. num);
	coroutine.yield(num += 1); -- This acts as a wait() but instead of passing a number it waits until whatever I put inside it is completed. Once complete it will continue the script.
end

(If you have previously coded in C#, JS, etc. You will find that you can use await inside of async functions coroutine.yield() is like an await but can be used anywhere and is a lua version of await.)

1 comment

0

Sorry, but this didn't work, and I know why it won't.. It's activating a bunch of outside scripts and it simply waits for them to finish, which is unfortunaley not gonna work.

This discussion is preserved as it was. New answers, comments, and votes are closed.