Instantly ending a long while loop

I’m trying to make it so whenever pressed is true the loop instantly stops even with the waits, I’ve tried using break, but it only does it at the end and I also tried using a for i= 1, second loop and it still doesnt work.

while pressed == false do
				target = oresfall.One
				--TweenService:Create(oresfall.One, TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {CFrame = CFrame.new(oresfall.One.Position, Vector3.new(oresfall.One.Position.X, oresfall.Two.Position.Y, oresfall.One.Position.X))}):Play()
				TweenService:Create(oresfall.One, TweenInfo.new(10, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					CFrame = oresfall.Two.CFrame
				}):Play()
				wait(10)
				TweenService:Create(oresfall.One, TweenInfo.new(5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					CFrame = oresfall.Three.CFrame
				}):Play()
				wait(3.5)
				local logo = script.Parent.Logo
				TweenService:Create(logo, TweenInfo.new(.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					ImageTransparency = 0
				}):Play()
				TweenService:Create(logo, TweenInfo.new(5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					Size = UDim2.new(0, 639,0, 427)
				}):Play()

				wait(2)
				TweenService:Create(logo, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					ImageTransparency = 1
				}):Play()
				local Menu = script.Parent.Parent.Menu
				TweenService:Create(Menu, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					BackgroundTransparency = 0
				}):Play()
				wait(1)
				local Menu = script.Parent.Parent.Menu
				TweenService:Create(Menu, TweenInfo.new(0.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					BackgroundTransparency = 1
				}):Play()
				oresfall.One.Position = originalpos

				target = Heart.One
				TweenService:Create(Heart.One, TweenInfo.new(10, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					CFrame = Heart.Two.CFrame
				}):Play()
				TweenService:Create(Heart.One, TweenInfo.new(5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					CFrame = Heart.Three.CFrame
				}):Play()
				wait(5)
				TweenService:Create(Menu, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					BackgroundTransparency = 0
				}):Play()
				wait(1)
				TweenService:Create(Menu, TweenInfo.new(0.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					BackgroundTransparency = 1
				}):Play()
				Heart.One.Position = originalpos2
			end
		end)

Try using a debounce. Your code should look something like this:

while pressed == false and debounce == false do
--run code here--
end

this should make it automatically stop

Lua need to do all loop and only then stop so only that you can do is -

while not pressed do
				target = oresfall.One
				--TweenService:Create(oresfall.One, TweenInfo.new(0.1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {CFrame = CFrame.new(oresfall.One.Position, Vector3.new(oresfall.One.Position.X, oresfall.Two.Position.Y, oresfall.One.Position.X))}):Play()
				TweenService:Create(oresfall.One, TweenInfo.new(10, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					CFrame = oresfall.Two.CFrame
				}):Play()
				wait(10)
                if pressed then return end
				TweenService:Create(oresfall.One, TweenInfo.new(5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					CFrame = oresfall.Three.CFrame
				}):Play()
				wait(3.5)
                if pressed then return end
				local logo = script.Parent.Logo
				TweenService:Create(logo, TweenInfo.new(.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					ImageTransparency = 0
				}):Play()
				TweenService:Create(logo, TweenInfo.new(5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					Size = UDim2.new(0, 639,0, 427)
				}):Play()
				wait(2)
                if pressed then return end
				TweenService:Create(logo, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					ImageTransparency = 1
				}):Play()
				local Menu = script.Parent.Parent.Menu
				TweenService:Create(Menu, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					BackgroundTransparency = 0
				}):Play()
				wait(1)
                if pressed then return end
				local Menu = script.Parent.Parent.Menu
				TweenService:Create(Menu, TweenInfo.new(0.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					BackgroundTransparency = 1
				}):Play()
				oresfall.One.Position = originalpos

				target = Heart.One
				TweenService:Create(Heart.One, TweenInfo.new(10, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					CFrame = Heart.Two.CFrame
				}):Play()
				TweenService:Create(Heart.One, TweenInfo.new(5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					CFrame = Heart.Three.CFrame
				}):Play()
				wait(5)
                if pressed then return end
				TweenService:Create(Menu, TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					BackgroundTransparency = 0
				}):Play()
				wait(1)
                if pressed then return end
				TweenService:Create(Menu, TweenInfo.new(0.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {
					BackgroundTransparency = 1
				}):Play()
				Heart.One.Position = originalpos2
			end
		end)

And also you can use TweenInfo reverses for TweenService.

1 Like