What are for loops

I’m not sure if this is the correct place to ask but what are “for loops”? I watched videos and still do not understand it. What is it’s practical use when making a game?

3 Likes

Hello. In summary, it allows you to run the code multiple times.

I think this will be useful. Introduction to Scripting | Documentation - Roblox Creator Hub

1 Like

Yes, but what’s it’s uses in games?

In the games: It is generally used to constantly check something, Increasing a continuous value, repeating etc.

for loops are useful for telling the computer to do the same operation multiple times.

They have a huge plethora of uses. Some examples include:

  • Doing something with all players in a game
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

for Index,Player in pairs(Players:GetPlayers()) do
    Player.Character:MoveTo(Workspace.Map.Spawns:FindFirstChild(tostring(Index))
end

In the example above, players in the game are all moved to various spawns in a map that’s parented to workspace. This is also a good example of something called iteration, where you loop through a data structure to do some operation.

  • Creating n copies of a game object, like a vehicle
local ServerStorage = game:GetService("ServerStorage")
local Workspace = game:GetService("Workspace")

local NumberOfCarsToSpawn = 10

for Count = 1,NumberOfCarsToSpawn do
    local NewCar = ServerStorage.CarModel:Clone()
    NewCar.Parent = Workspace
    print("Spawned car #" .. Count)
end

The above code would spawn 10 cars into the game’s world.

5 Likes

A for loop runs a loop block a specified number of times. It is used in games whenever you have to have to repeat something more than once.

Commonly, there are two types of “for loops” used.


The first common “for loop” is

for count = 1, 3, 1 do 
--stuff
end

“count”
is a control variable. You can change it to whatever you want.

“1”
is the start number. It’s the number the “for loop” starts at.

“3”
is the end number. It’s basically the opposite of the start number.

The second “1”
is the increment. This is the number that the loop will count up by. A positive value will count up while a negative value will count down. Additionally, if you don’t include the increment, the default increment (1) will be used.


The other common “for loop” is

for i, v in pairs(t) do
--stuff
end

“i, v” are
both variables. You can add more than two.

pairs is
an iterator. Use “pairs” if you’re looping through a dictionary.
Example of a dictionary: local dict = {a = 1, b = 2}
If you’re looping through an array, use “ipairs”
Example of an array: local ary = {1, 2, 3}

t is
what you’re looping through. If you want to loop through an instance, you can loop through
Instance:GetChildren() or :GetDescendants()


Practical and common uses of these in actual games

I haven’t scripted any full on games yet, so I don’t know all about practical uses of “for loops” in games.
However, a usual thing I use “for loops” for is to loop through a model and change it’s properties.

for i,v in ipairs(game.Workspace.Model:GetChildren()) do
    if v:IsA("Part") then
v.Transparency = 1
    end
end

You can also use the first “for loop” I explained to make things such as countdowns.

for number = 60, 1, 1 do 
print(number)
if number == 1 then break

I’m not an advanced programmer so some things in this post might be incorrect.
Hope this helps!

7 Likes

So, say I made a game where I need to check how much players were alive in the game so I can reward them. How would I use a for loop for that? Or it’s not applicable in that scenario

Certainly helped my man, but I have a question. If I had a game where I want to check how many players are alive so I can give them xp, etc. is a for loop useful? Or is there another code I should go for

I think, here you have to use Table.

This is 100% possible using for loops.


First, you can create two teams; Dead and Alive
obviously, you can change the names of these teams and add more teams


Before the next round starts, you could move all the players in the game into one team; Alive

for i,v in ipairs(game.Players:GetChildren()) do
	v.Team = game.Teams.Alive
end

Then, you could move a player into the Dead team if their Humanoid.Health ever reaches 0.

while true do
	for i,v in ipairs(game.Players:GetChildren()) do
		if v.Team == game.Teams.Alive then
			if v.Character.Humanoid.Health == 0 then
				v.Team = game.Teams.Dead
			end
		end
	end
end

Lastly, at the end of the round, loop through the Alive team once again and give them the rewards.

for i,v in ipairs(game.Players:GetChildren()) do
	if v.Team == game.Teams.Alive then
		--give them rewards or whatever
		print(v.Name .. " was given a reward!")
	end
end

You can use RemoteEvents and Functions to alert the script that the round is over.
Additionally, you can use DataStores to create an xp/rewards system.


This is just an overview of what a basic system that you’re describing could potentially look like. Enevitably, there is always an endless amount of possible improvements but this should get you on the right track.

2 Likes