What's a good scenario for a "for loop"

i’m wondering when you would use the for loop

for i = 1, 10, 3 in pairs
-- code
end

i understand 1 is the beginning, 10 is the end, and 3 is the increment but when would someone use this type of for loop?

i only know how i would use

for i, v in pairs()
-- code
end

Here are 5 examples I usually use these for:

  1. Looping through an array with the index.
    e.g.
local arr = {1,4, "string"}
for i = 1,#arr,1 do
    print(arr[i] .. "is number" .. i)
end
  1. Countdowns, like intermissions for games.
    e.g.

for i = 10,0,-1 do
    print(i .. " seconds left!")
    wait(1)
end
  1. Typewriter effects with GUI, looping to the string’s length and adding a character each time.
local str = "Hello this is my string"
local loopStr = ""
for i = 1, #str,1 do
    loopStr = loopStr .. str:sub(i,i)
    print(loopStr)
end
  1. If I want to do a specific actions numerous times.
for i = 1,10,1 do
    print("I want this to repeat 10 times")
end
  1. Lerping CFrames and making things gradient like TweenService.

Apparently, looping with the increment is faster than the in-pairs or next.

1 Like

image
Lets say I had these parts in the workspace and I didn’t wanna have a script inside of everyone of them checking if they were touched I could just use a for loop for example:

for i,v in pairs(game.Workspace.Parts:GetChildren())do
	if v:IsA('BasePart')then
		v.Touched:Connnect(function(Player)
			if game.Players:GetPlayerFromCharacter(Player.Parent)then
				v.Transparency = 0
			end
		end)
	end
end
1 Like

If you play a game like Roblox piggy… and watch the guis of the npc and cutscenes, then you should see a little type writing effect. Which is quite helpful if you want to add detail and engage your players.

1 Like

thank you for the example. do you have one for

‘’‘lua
for i = 1, 10, 3 in pairs
–code
end
‘’’

thank you for this link. it’s very helpful.

1 Like

I like to use for loops with moving bricks. Here’s an example:

local brick = script.Parent

for i = 0, 21 do
brick.Position = brick.Position + Vector3.new(0,0,0.5)
wait(0.01)
end

or… I use for loops for specific indexes. For example, a fade out script.

for i = 0, 1, 0.1 do
brick.Transpaency = i
wait(0.01)
end

2 Likes

Using tween service for things like that would be way more effective. But for beginners thats not bad!

1 Like

in that example, is it going to times the position amount by what you put however many times you have?

Yes my apologies, you can use a + instead of a *. LuaGoats, I know haha. I figured if they’re a beginner then I would show them the easy way first.

1 Like

so for the transparency one it would go up by .1 until it gets to 1? i think im starting to understand it now.

It’s useful for mathematical iterations or sequences that you want to iterate by a specific amount instead of using an epsilon for accuracy, such as a fibonnaci sphere or tilings.

3 Likes

Yes, so for the first loop, the transparency is 0.1, then for the 2nd loop it’s 0.2, the third is 0.3, etc…

1 Like

yeha i was gonna say haha might crash or something. idk bout crash but it would get pretty crazy.

im starting to understand! breakthroughs bro haha. i love scripting. so what whats a good example if you were getting information? i cant think of a good scenario

Nope, it works totally fine haha. I also made a cool one where you can dim all the lights in your game.

for i = 255, 0, -1 do
game.Lighting.Ambient = Color3.fromRGB(i,i,i)
wait(0.01)
end

1 Like

oh okay. thank you for the information!

dude you can do -1 as increment and have the start be a high number then low number. thats cool. yo everone is being so helpful. thank you. so much value.

Yessir! A good starting point is make a game where if the player touches a brick, they start to fade away. Or you can do is you can put a bunch of parts in a folder, use a for i, v in pairs loop and have them fade away periodically. That’s a solid example.

1 Like

oh that’s awesome. i’m starting to understand more. i’m going to write some scripts to better understand for loops. this already has been so helpful. i now understand a lot more.