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
3 years ago

Hello,

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

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

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 — 3y

2 answers

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

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

repeat Character.Humanoid.WalkSpeed = 22 wait()
 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
3 years ago
Edited 3 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:

while Degrees <= 45 and DeltaRotation.magnitude < 8 do 
    wait()
    repeat 
        WalkSpeed = WalkSpeed + 0.5 
        wait()
        print(Character.Humanoid.WalkSpeed)
    until Degrees > 45 or DeltaRotation.magnitude >= 8
end

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:


-- 1st if statement if Degrees <= 45 and DeltaRotation.magnitude < 10 then for i = Character.Humanoid.WalkSpeed, 22, 0.5 do -- LOOP Character.Humanoid.WalkSpeed = i if Degrees > 45 or DeltaRotation.magnitude >= 10 then break end wait(0.2) end -- 2nd if statement elseif Degrees >= 101 and Degrees <= 180 and DeltaRotation.magnitude <= 220 then Character.Humanoid.WalkSpeed = 12 end

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 — 3y
0
Hi Eagle, I've placed a larger section of the code above. 0te 35 — 3y

Answer this question