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

What would be the correct way of breaking a loop?

Asked by
0te 35
4 years ago

Hello,

The value of Degrees and DeltaRotation are constantly changing as this whole loop is in a renderstepped function.

1for i = Character.Humanoid.WalkSpeed, 22, 0.5 do
2    if Degrees > 45 or DeltaRotation.magnitude >= 8 then
3        print("broken")
4        break
5        else
6        Character.Humanoid.WalkSpeed = i
7    end
8end

However, when the Degrees and DeltaRotation meet the requirements of the if statement, the output does not print "broken". Instead the WalkSpeed increases.

I appreciate anyone willing to take the time to point me in the right direction in to why this loop may not be breaking.

0
Where's the full script? I want to see where you defined the variables not just  the if statements JesseSong 3916 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You should try using repeat.... until... like:

1repeat Character.Humanoid.WalkSpeed = 22 wait()
2 until Degrees > 45 or DeltaRotation.magnitude >= 8

Edit: I forgot about the wait(), its required to prevent a timeout!

Ad
Log in to vote
0
Answered by
0te 35
4 years ago
Edited 4 years ago

Thank you, I've just looked in to repeat and until and also while and do loops.

I've fiddled around with the code and took in to account what you said and ended up with the below:

1while Degrees <= 45 and DeltaRotation.magnitude < 8 do
2    wait()
3    repeat
4        WalkSpeed = WalkSpeed + 0.5
5        wait()
6        print(Character.Humanoid.WalkSpeed)
7    until Degrees > 45 or DeltaRotation.magnitude >= 8
8end

The code shows no errors however my walkspeed doesn't appear to be changing.

EDIT:

Here is a larger section of the code. I've reverted to the following code which sort of works:

01-- 1st if statement
02if Degrees <= 45 and DeltaRotation.magnitude < 10 then
03    for i = Character.Humanoid.WalkSpeed, 22, 0.5 do -- LOOP
04        Character.Humanoid.WalkSpeed = i
05        if Degrees > 45 or DeltaRotation.magnitude >= 10 then
06            break
07        end
08        wait(0.2)
09    end
10 
11-- 2nd if statement            
12elseif Degrees >= 101 and Degrees <= 180 and DeltaRotation.magnitude <= 220 then
13    Character.Humanoid.WalkSpeed = 12
14end

The problem I'm having is that when the 2nd if statement is triggered, the loop in the 1st if statement continues to finish but I want it to break as soon as the 2nd if statement is triggered.

0
can you show me the full script? TheEagle722 170 — 4y
0
Hi Eagle, I've placed a larger section of the code above. 0te 35 — 4y

Answer this question