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

How do you go about yielding in a while loop?

I've heard that if you do not yield in a while loop, it tends to break. In matter fact, I believe it was one of this websites tip of the day. In my game, my round script breaks after a couple of rounds, so I'm wondering if this is the issue. Anyways, can anyone explain how you yield in a while loop? Thanks! :)

3 comments

0

You should show your script to us so we will be more likely to help you!

0

You should probably edit your question to include the script you're asking about; I don't think that would be your problem.

1 answer

✓ Accepted answer

By yield, they meant that your script will stop executing for a while before continuing. This is most commonly done with a wait command, like this:

local n = 0
while true do
	wait(1)
	n = n + 1
end

If you were to take out the wait(1), you would be instructing lua to stay in the loop forever -- which means that Roblox wouldn't be able to update physics, display graphics, or run other scripts. This is why Roblox usually freezes when you have a loop like this.

Other ways of yielding include coroutine.yield(), calling any function that is labelled as a yield function (ex DataStore's YieldFunctions), or ending the thread (via 'error' or just finishing executing the code).

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