Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Would someone mind explaining "for loops" to me?

while true do
	for _, child in pairs(children) do
		if child.Name == "ExplosionPart" then
			ExplodePart(child)
		end
	end
	wait(1)
end

I'm on the ROBLOX wiki, learning scripting basics and such, and I have come across the for loop tutorial and it doesn't explain what it is very well. Would someone mind explaining it? Thank you!! :)

2 answers

✓ Accepted answer

A for loops iterates (goes through) each of the values in a table. Say there was a table containing all the instances (objects) in workspace and you wanted to check if it was a part and if it is then make it a random brick colour.

The script would look something like this (There is an underscore where 'i' is because we don't need the index):

for _, part in pairs (workspace:GetChildren()) do
	part.BrickColor = BrickColor.Random()
end

Keep in mind values in a table could be anything! For example it could consist of children of a model, integers (whole numbers), strings (text) and etc. When using a for loop there are two variables you put in. The index and the variable (usually seen as i, v). The index is the index in a table (its quite hard to explain but if you see the wiki you'd understand, the value is basically just the value. I know it sounds really complicated but say there was a table with all the children of a model. There would be no index only the values, and in this case the value would be the part. So because we dont need the index we could replace the i with an underscore and change the v to part.

It would look something like this:

for _, part in pairs (model:GetChildren()) do
	print(part.Name)
end

Hope this was easy to understand, feel free to accept this answer.

2 comments

0

Oh I found out that the index is actually the amount of times it ran, so its quite similar to for i = 1 loop, people also do for i = 1, #children do but that isn't necessary.

0

That depends on the functionality of the loop. In some cases, that is useful.

Loops in programming are repeated code. For loops are used to execute said code a specific number of times.

for a = 1, 10 do
	print(a)
	wait(1)
end

This script will print "a" ten times because it starts at the value of 1 and then increases by one each time the code runs through. Therefore, your output will look like this:

1 >> 2 >> 3 >> 4 >> 5 >> 6 >> 7 >> 8 >> 9 >>10

The pairs and ipairs keywords are used to iterate a table. In the example you provided in the question, the table is "children" and the for loop will be executing the code equal to the number of indexes in the table (or variables in other words), for each of those variables.

a = {1,2,3}
for i,v in pairs(a) do
	if v == 1 then -- v is equal to the value inside the table that the script is currently acting upon
		print("The value of one has been found in the table")
	end
end

3 comments

0

So, in the second code you put, where it says "i, v in pairs(a) do", can "v" be anything?

0

Nevermind I see my mistake

0

Anything that isn't a keyword.

This discussion is preserved as it was. New answers, comments, and votes are closed.