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

For loops going backwards with subtraction?

I can't help but wonder if for loops can go backwards. Let me show an example with my code here.

part = script.Parent

function sum(x, y, z)
	for i = 1, 10 do
		part.Position += Vector3.new(x, y, z)
		wait(0.01)
	end
end

while true do
	sum(1, 0, 0)
	sum(-1, 0, 0)
end

I figured that if this for loop reaches its end/max value, it will stop and the while loop will call the next function and the for loop will go backwards by subtracting. Is this true? To be specific here, I thought for loops could only go forwards, because when I remove the second function call "sum(-1, 0, 0)", the for loop will keep going non-stop. I don't get it, I need to understand how this works. And sorry, I'm new to programming.

1 comment

0

Well 1 thing is you shouldnt put that inside a while loop. What are you trying to make exactly?

1 answer

✓ Accepted answer

Hi, for loops have another feature that allows it to go backwards, your 3rd number states whether it goes forward or backward. By deafault, for loops think the 3rd number is 1 making it for 1 forward each time.

For example:

for i = 100,1,-1 do
  print(i)
end

The third number makes it go 1 back each time, from 100 until it reaches 1

Hope this helped!

Any questions? Just ask!

2 comments

0

Thank you very much!

0

np

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