Before we begin, if you’d like to join the google classroom please check out our discord server.
Creating Loops In Roblox Lua:
There are multiple ways to create loops in the lua language and roblox scripting.
Important: Loops always begin with the word ‘while’ unless you are using another method such as ‘repeat’Definition of ‘while’ according to scriptinghelpers.org:
Warning: Improper use of loops can result in game lag. To prevent game lag, always use at least 1 wait statement in your loop code. Otherwise your loop may not even function correctly either."The
while
keyword creates a loop and must be used in combination with the do keyword.Between the
while
anddo
keywords is the condition that the loop must meet to execute. The loop is ended with the end keyword. Everything in between thedo
andend
keywords is what is executed in the loop.The
repeat
loop is similar to thewhile
loop.while
checks the condition before executing the first time, and stops when the condition isfalse
."while isRaining() do useUmbrella() end
Here are some other examples:
while true do
wait(1) -- used to prevent game lag, and as a counter before the code below is executed.
game.Players.LocalPlayer.Character.Humanoid.Health = 0 -- kills the player, this line
-- can ONLY be used in local scripts
end
The previous code is used to kill the player every second.
that = 2 -- a variable
while that >= 1 do
print(that) -- prints out the variable value in the output.
that = that + 1 -- adds one to the 'that' variable.
end
This code will check if a certain variable is equal to or greater to the number ‘1’ and if it is then it will execute the code. If it is not, then it should do nothing. But it will still check if this is right or not every so often. It is still suggested to have a wait statement here too. If you understand how variables work, you will know this can apply to a check for leaderstats and values.
NOTICE: Using the above method is great, but is never a good substitute for creating if then statements. You should use these kinds of statements for that instead:
that = 2
if that >= 1 then
print(that)
that = that + 1
end
Clearly this code will only execute once as there is no loop.
This code also completes the same task but uses a different method of creating loops.
while wait() do
game.Players.LocalPlayer.Character.Humanoid.Health = 0
end
In these kinds of code, a counter is usually not needed. But you can add a counter if you want and it will work like the code above.
Do you notice a similar setup to the scripting helpers reference code?
If you read the did you know below, you will know that in most coding languages you can create custom code tags and setups. This is useful for quickly creating code, like variables are. You can learn more about these custom functions in a later article: (the link goes here)
? Did you know: Technically you can create any tags or code and design it how you want, but read up on the programming language you are using and understand how most scripts you see are designed before publishing any updates involving these test scripts. Always bug test before releasing a new update, because scripts can sometimes cause uncalled for errors. And most errors aren’t recorded in the output / dev console (F9 for dev console in-game and go to view tab in studio for output, they are the same thing.)
Q: What types of scripts can these loops be placed in?
A: Any. Both LocalScript and Script. Expect the ModuleScript because well… reasons, as it’s not really designed for code, and instead used to store information. Such as a gun’s stats.
Let’s talk repeat functions:
What are repeat functions? Here is a code example:
pod = 4
repeat
print(pod)
pod = pod + 1
until pod > 10
This code will repeat the code below the ‘repeat statement’ until the pod variable is greater than 10. The loop will then stop.
Ending Loops
You can simply put a ‘break
’ at the end of your loop like shown to end a loop.
while true do
wait(1) -- used to prevent game lag
print("hi there") -- prints 'hi there' in the output.
break -- 'breaks' the loop. Basically ending the loop.
end
This is not usable in repeat functions.
Resources/Sources:
Last Updated: 2/1/2020
Wrong information? Need help on using loops? Code not working correctly? Want to add code examples? Contact me on my discord server or discuss it in the classroom!
Thank you for reading this article.