Previous Beginner Lua Lesson

First Beginner Lua Lesson

Today, we’re learning about Loops. There are 4 types of Loops. Each can serve their own purpose, but all can perform certain functionality.

while true do

A while Loop will keep going as long as the value in between while and do is true. This means you can do this with number values, and even this if you’re bored: while not false do. Be sure to use a wait with this loop, otherwise your server will lag very hard. In fact, it will be nearly unplayable, especially if the Loop contains a lot of functionality. This explains the inside joke between ROBLOX Developers, while true do end. Even some non-programmers understand this joke.

Jokes aside, this is a good loop to use if you’re looking to repeat a certain function infinitely. As well, this Loop must have an end. Finally, you can always stop the loop regardless of the value being true through the usage of break. Here’s an example of breaking a loop of raining bricks after number becomes 100:

local number = 0
while true do
local part = Instance.new("Part", workspace)
part.Position = Vector3.new(math.random(0,100),100,math.random(0,100))
number = number + 1
if number == 100 then
break
end
end

repeat wait() until

This is very similar to the while Loop. It will repeat what is in between repeat and until until the following value is what you need it to be. Just like using if … then, you check a value/Instance the same way after until. As you can see, using wait is also very wise with this loop. Example:

game.Players.PlayerAdded:connect(function(player)
print("Waiting for "..player.Name.."'s character to spawn...")
repeat wait() until player.Character ~= nil
print("Character spawned!")
end)

for i = BeginningNumber, EndingNumber do

This is a loop that goes in order from the beginning number, to the ending.  “i represents what is passed into the loop each cycle. This value’s name can be changed. Just like the while loop, you can use break on this loop anytime, and using wait is highly recommended. This serves very many useful purposes. Finally, you can add a third value after EndingNumber that allows you to change the intervals. This can be used with things such as smoothly fading text. Example:

for i = .1, 1, .1 do
script.Parent.TextTransparency = i
wait(.1)
end

for i, v in pairs(table) do

This loop is purely for tables. “i” serves as the position v in the table. Both of the names on these can be changed. “v” represents the object in the table currently in the loop. This can often be a much more efficient way of using tables in a loop, compared to the for i = BeginningNumber, EndingNumber do loop. This is because you do not need to use wait with this loop. It will go through an entire table without causing lag at a very high speed. This can be used for teleporting players all at once to the same location. Example:

--The GetPlayers method returns all the players into a table--

for i, player in pairs(game.Players:GetPlayers()) do
if player.Character ~= nil then --Check if they're spawned to prevent errors
player.Character.Torso.CFrame = Vector3.new(1,33,7)
--You must change the Character's Torso's CFrame to properly teleport them--
end
end

Confused or just have questions? Shoot me a PM on ROBLOX or leave a comment!

Trending