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

What would be the correct way of breaking a loop?

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.

1 comment

0

Where's the full script? I want to see where you defined the variables not just  the if statements

2 answers

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!

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.

2 comments

0

can you show me the full script?

0

Hi Eagle, I've placed a larger section of the code above.

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