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

Why can't I break my loop in my script and goes on forever when there is no error?

I made script that does a loop for a limited amount of times before i stops but it continues to go on forever why isn't it working?

local Times = 10

while Times > 0 do
	if Times > 0 then
		script.Parent.bodyVelocity.Velocity = (script.Parent.CFrame.LookVector * 10)
		wait(0.1)
		Times = Times - 1
		print("Seconds remaining: "..Times.."")
		wait()
	end
end

3 comments

0

add an else then write break

0

also mark this question as solved

0

How do I mark as solved

2 answers

I figured it out myself I made an edit to my script and now it works perfectly

local Times = 5

while true do
	if Times > 0 then
		script.Parent.bodyvelocity.Velocity = (script.Parent.CFrame.LookVector * 10)
		Times = Times - 1
		wait(1)
	else
		script.Parent.bodyvelocity.Velocity = (script.Parent.CFrame.LookVector * 0)
		wait(1)
	end	
end

You can break the while loop after a certain amount of time.

local Times = 10

while Times > 0 do
    if Times > 0 then
        script.Parent.bodyVelocity.Velocity = (script.Parent.CFrame.LookVector * 10)
        wait(0.1)
        Times = Times - 1
        print("Seconds remaining: "..Times.."")
        wait()
        break -- Breaks the while loop when it runs only once.
    end
end

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