Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

For loops going backwards with subtraction?

Asked by 3 years ago
Edited 3 years ago

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.

0
Well 1 thing is you shouldnt put that inside a while loop. What are you trying to make exactly? Cirillix 110 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

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!

0
Thank you very much! eliyagamer5 2 — 3y
0
np sne_123456 439 — 3y
Ad

Answer this question