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”
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.
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
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!
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?
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
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.
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
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.
@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