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

I am trying to reverse a for loop?

Asked by 10 years ago

So my first one was this

if k == "b" then
char.Torso.Anchored = true
char.Humanoid.WalkSpeed = 0
coroutine.resume(coroutine.create(function()
for i=0,0.2,0.08 do
wait()
char.Torso.CFrame = char.Torso.CFrame * CFrame.new(0,180*i,math.rad(340*i))
end
end))
coroutine.resume(coroutine.create(function()
for i=0,0.5,0.08 do
wait()
char.Torso.CFrame = char.Torso.CFrame * CFrame.Angles(math.rad(180*i),0,0)
end
end))
wait(0.25)
char.Torso.Anchored = false
char.Humanoid.WalkSpeed = 20

Then my second one I tried to reverse it like this but it does not work it still goes the other way.


elseif k == "f" then char.Torso.Anchored = true char.Humanoid.WalkSpeed = 0 coroutine.resume(coroutine.create(function() for i=0.2,0,-0.08 do wait() char.Torso.CFrame = char.Torso.CFrame * CFrame.new(0,180*i,math.rad(340*i)) end end)) coroutine.resume(coroutine.create(function() for i=0.5,0,-0.08 do wait() char.Torso.CFrame = char.Torso.CFrame * CFrame.Angles(math.rad(180*i),0,0) end end)) wait(0.25) char.Torso.Anchored = false char.Humanoid.WalkSpeed = 20

I am simply trying to reverse the second one but the method I used did not work..

1 answer

Log in to vote
0
Answered by
um3k 10
10 years ago

Please, for the love of all that is holy, indent your code. It's easy for you to see what's going on, because you wrote it, and it's fresh in your mind. The rest of us have to do some deciphering.

With that said, I think I see your problem. Well, a few problems.

I assume you are moving the character upwards in the first part ("B"), and downwards in the second part ("F").

In B, your loop repeatedly uses the * operator on the Torso CFrame. This is a cumulative action, it adds to itself every time, since you are reusing the Torso.CFrame as input.

The first iteration adds 1800.08 to the Y coordinate; the second adds 1800.16, for a total change of 1800.24 from the original value; the third adds 1800.24, for a total change of 1800.48, and so on. This results in acceleration, which is fine, if that's what you are trying to achieve.

Uh, actually, that's all it does. Maybe not even that. The maximum value for your for loop is 0.2, which is 0.082.5. So your loop only iterates 2-3 times, not sure which, specifically.

If acceleration is not your goal, you'll want to either store the original CFrame in a variable, and add 180*i to that every iteration; or simply add a fixed number to Torso.CFrame every iteration.

A minor point, I'm not sure why you are using math.rad() inside CFrame.new. You're converting degrees to radians, and then using it as a positional coordinate.

Also, it's not necessarily wrong, but it is fairly unusual to use floats/decimals in your for loop. Generally, I would use integers, and scale them if need be. It's easier to understand what's happening.

However, those are all nitpicks, not the problem you are asking for help with. This is:

The problem with F is that you're doing the exact same thing as you are in B, only backwards. You're not doing the opposite, you're adding the same total amount, in a different order. What you need to do is change the starting number to 0, and the end number to -0.2 (or -0.5, in the second loop). That ought to give you something closer to the expected results.

0
Ah, and by the way, the first code is a back flip, the first for loop making you go in the air, and the second one making you actualy do the flip. :p I will try what you said( and because for loops as small as decimals are allot smoother for animation, and math.rad is fairly easy to work with). RootDirectory 10 — 10y
Ad

Answer this question