Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you go about yielding in a while loop?

Asked by 7 years ago

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! :)

0
You should show your script to us so we will be more likely to help you! starlebVerse 685 — 7y
0
You should probably edit your question to include the script you're asking about; I don't think that would be your problem. Pyrondon 2089 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

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).

Ad

Answer this question