What does "for i" do?

So I am still learning lua and I already learned stuff like raycasting and functions and other basic and a little bit more advanced stuff, but what I can’t seem to understand is that if I am watching a tutorial I don’t know what for i means. Here is something I saw in a tutorial:

local plrs = game.Players:GetChildren()
for i = 1, #plrs do
local num = math.random(1, 32)
plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Part"..num].Position)
end

I understand most of it and know what it does but the only thing I don’t know is the “for i”

Please help

10 Likes

A for loop will loop continuously until the iteration, or i, is equal to the final designated iteration, which is #plrs in this case. #plrs is the count of elements in a table from game.Players:GetChildren().

i can be assigned at a different value for a different “start” position and a third value may be assigned to determine the iteration step. By default, the step is +1 which means every loop, i += 1.

3 Likes

It’s a numerical loop that goes from a starting number given till the ending given number,

for i = 1, 5 do

basically means to do what’s inside of the loop 5 times, you can even use that variable made to do something,

for i = 1, 5 do
   print(i) --Prints 1 then 2 then 3 and so on
end

There’s an optional 3rd value that can be given which is the increment the loops increments with, by default if you don’t give anything, it assumes you want to icnrement by 1

for i = 0, 10,2 do
   print(i) --Prints 0 then 2 then 4 and so on
end

--Also works with decrementing!

for i = 10, 0, -1 do
   print(i) --Prints 10 then 9 then 8 and so on
end

That code you gave basically just gets the children of the players service and makes a loop from 1 to the total amount of players. Then it gets a player using the current value of i and then changes the CFrame of the head

25 Likes

Ooh I understand now, this is helping me out! Thanks!

1 Like

Anytime! If you have anymore issues don’t be afraid to make another post!

4 Likes

Also thank you for trying to help me out, to be honest this response was still a bit confusing for me but now with the other response I understand. Thanks!

Basically, it iterates from the first value to the second value. The third value tells you how to much to step, or go up/down in.

-- Example 1
for i = 1, 10 do
print(i) -- Prints 1, 2, 3, 4, and so on
-- Example 2
for i = 10, 1, -2 do
print(i) -- Prints 10, 8, 6, 4, and so on
1 Like

so if the script is

local plrs = game.Players:GetChildren()
for i = 1, #plrs do
local num = math.random(1, 32)
plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Part"..num].Position)
end

Then it goes from 1 to the amount of players and teleports them to the part right? (parts are named Part1 to Part32)
So they teleport player number 1, then player number 2 until they have reached the player count?

Yep, it’s kind of like a

for i, v in pairs(game.Players:GetPlayers()) do

end

Yes, it gets the total amount of players, and loops from 1 to the total amount of players, and every iternation. It gets a random number between 1 and 32 and changes the CFrame of the current player’s head to the position

It’s the same as

local plrs = game.Players:GetPlayers()

for _,player in pairs(plrs) do
	player.Character.Head.CFrame = workspace.Teleports["Part"..num].CFrame
end
1 Like

I don’t understand what “v” does and “pairs”, even tho I read about pairs on the developerhub website.

what is “_” Is that the same as “v”?

I don’t understand this lol

1 Like

Be careful of using that code. Apparently it will fail as soon as a player joins and then that player is not even loaded in with a character.

1 Like

It is a round system for a minigame so if the player joins and the character doesn’t load in that means he is too late.

Alright, so basically it does this.
The “i” defines the index, or basically the number the value is located in the table.
The “v” defines the value.
“pairs” lets you loop through a table.
So combine 'em all together:

local MyTable = {
"Hello",
"World!"
}
for i, v in pairs(MyTable) do
print(i.." is equal to "..v.."!")
end

PS: _ basically does nothing. So if I put it in the “i”'s position, it implies I have no use for the index in the current loop.

1 Like

What was mentioned was an in pairs loop, it’s another type of loop that goes through a table, the first thing given is the index of the current step it is in and the value in that index

local fruits = {"Apple","Banana","Pineapple"}

for i,v in pairs(fruits) do
	print(i,v) -- Prints 1 Apple then 2 Banana then 3 Pineapple
end

Also works with Dictionaries but you’ll get into that later

Also, I recommend you check if the character exists first via a if plrs[i].Character then if statement before settign the CFrame of the head

1 Like

It will certainly cause an error of Attempting to index nil with Head.

The use of v means value. After each iteration(or step), a value from the argument within pairs() is used and the index is incremental from 1 to n elements but if it was a dictionary, the indexes are keys which are often strings.

1 Like

Alright I understand now! Thanks all of you for responding!

1 Like

You’re welcome! If you have anymore questions or issues don’t be afraid to make another post!

1 Like

@EmbatTheHybrid explained it very well, for a more in depth tutorial, I suggest going on @Alvin_Blox YouTube channel and watching his tutorial or for loops, while you’re there, you can also (if you have the time) watch his tutorial on for i, v in pairs

1 Like