What are for i loops and how do they work?

So im trying to create a rain mechanic, and my friend said i should try using a “for i” loop.

Im not exactly sure what a for i loop is and a somewhat simple explanation would be appreciated.

Heres the script if anyones interested

local NewPart = Instance.new("Part", game.Workspace)
local Debris = game:GetService("Debris")

local random1 = math.random(0,249)
local random2 = math.random(0,149)

local newCFrame = CFrame.new(random1,164,random2)
--for i loop here(?)
NewPart.CFrame =NewPart.CFrame * newCFrame
NewPart.Name = "Rain"
Debris:AddItem(NewPart, 15)
--end

Im still learning how to script, and i want to learn more so i dont want to be spoonfed scripts

1 Like

Since you are very new to coding I recommend you check out
https://education.roblox.com/en-us/resources/intro-to-coding-coding-3-repeating-tasks-with-for-loops
On the left is I side bar, I recommend you read everything in Loops and Working With Tables

2 Likes

im not good at explaining but i’ll try. For is a loop where it repeats until it ended.
Example:

for (Variable Name) = random1, random2, --(Adding value. Add a negative sign so it would subtract if the starting value is higher than ending value) do
-- script 
wait() --Seconds until it repeats again
end

I think you should make random1 local random1 = math.random(150,249).
The script will not proceed until for do is finished

1 Like

What would i put inside the variable?

i want the script to run infinity without using a while loop

You could name the variable “i” so you could make the script print(i) – prints numbers from random numbers.

for do is not a forever loop cause you need to put the ending value to it.

1 Like

although i should just send u da link,
but ill try to explain it by myself:

for i = 1, 10 do
print(i) -- 1 2 3 4 5 6 7 8 9 10
end
for i = 1, 10, 2 do
print(i) -- 1 3 5 7 9
end
for countin = 5, 1, -1 do
print(countin) -- 5 4 3 2 1
end
for banana = 9, 4, -3 do
print(banana) -- 9 6
end

to summarize, a for i loop’s structure is

for [index name] = number a, number b, number c do
[stuff]
end

it will run [index name] from a to b ( or b to a if b>a, in dis case, number c is da decreasemet and is required for da loop to run ( c<0) )
each time it does, it will run [stuff]. if a<b, then number c ( increasement ) is optional

here is da link, btw:
Introduction to Scripting | Roblox Creator Documentation

1 Like

Here’s a simple explanation:

for i = 1, 10 do --i stores the current number.1 is the first number to be counted and 10 is the last.
   print(i) --since i stores the number, it would print
   --1
   --2
   --3
   --etc.
end --This closes the for loop.

--You can also do
for i = 1, 9, 2 do --2 is the skip count, so it would print 1, 3, 5, 7, 9
   print(i)
end
1 Like

r u there dud, we have probably created a dictionary of for i loop lol

1 Like