Still have questions? Join our Discord server and get real time help.
1

# Is there a way to go to the start of a loop before it ends?

Asked by 2 months ago

I don't think you need a description of what I'm making, so I won't waste your time with that (If you want to know then feel free to ask).

I'm wonder if it is possible to go back to the start of a while true loop before the end of it. An example of this would be

while true do
[scripting]
If [blank happens] then
[go back to start of while true loop]
end
end


If this is possible I would love to hear how. If not I guess I'll have to figure something out.

0
Answered by 2 months ago

Functions!

The best way to go about this would be to define a function, and continuously call it from either inside of itself, or in a loop, depending on your preference.

Confusing? Let me break down the code in English. First off, you would start by creating a function, let's call it SomeRandomAsync(). This can be whatever you want it to be, it won't affect how the code runs at all.

Next, we will put our [scripting] as used in your code snippet at the beginning of the function. If you can see where I am going like this, it is structured just about the same as the loop you had initially.

Last, add your conditional. If the condition (blank happens) is true, then you can call the function again. If the condition is not true, it will exit all of the functions that it has been in automatically without needing to break it.

Let's have a look at an example!

function SomeRandomAsync()
[scripting]
if [blank happens] then
game:GetService('RunService').Stepped:Wait() --Not trying to confuse you or anything, this is just a different type of 'wait(),' but it fires more often than 'wait(),' increasing the efficiency of your game. Do note that the wait is necessary, as in case the conditional is not true for a while, your script could exhaust/timeout, especially if it runs in the beginning.
SomeRandomAsync()
end
end

SomeRandomAsync() --Obviously we need to call it or it won't run.


Replace [scripting] with the scripts that need to run, and replace [blank happens] with the conditional that should be met for the function to run again.

If you have any questions, comments, concerns, or errors, please leave a comment below. Other than that, Happy Coding! :D