How can I end this while loop?

I’m currently writing a script that tweens a player’s camera in different locations but when I want the looping between locations to stop, the script keeps running the loop even if the script is destroyed using :Destroy().

Here is my script:

while Player.Play.Value == false do
	wait(12)
	while Player.PlayerGui.HomeScreen.Fade.Transparency > 0 do
		Player.PlayerGui.HomeScreen.Fade.Transparency = Player.PlayerGui.HomeScreen.Fade.Transparency - .05
		wait(.05)
	end
	tweenInfo = TweenInfo.new(
		20,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	tween(game.Workspace.CameraTownA, game.Workspace.CameraTownB)
	wait(1)
	while Player.PlayerGui.HomeScreen.Fade.Transparency < 1 do
		Player.PlayerGui.HomeScreen.Fade.Transparency = Player.PlayerGui.HomeScreen.Fade.Transparency + .05
		wait(.05)
	end
	wait(17)
	while Player.PlayerGui.HomeScreen.Fade.Transparency > 0 do
		Player.PlayerGui.HomeScreen.Fade.Transparency = Player.PlayerGui.HomeScreen.Fade.Transparency - .05
		wait(.05)
	end
	tween(game.Workspace.CameraMuseumA, game.Workspace.CameraMuseumB)
	wait(1)
	while Player.PlayerGui.HomeScreen.Fade.Transparency < 1 do
		Player.PlayerGui.HomeScreen.Fade.Transparency = Player.PlayerGui.HomeScreen.Fade.Transparency + .05
		wait(.05)
	end
	wait(12)
	while Player.PlayerGui.HomeScreen.Fade.Transparency > 0 do
		Player.PlayerGui.HomeScreen.Fade.Transparency = Player.PlayerGui.HomeScreen.Fade.Transparency - .05
		wait(.05)
	end
	tweenInfo = TweenInfo.new(
		45,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	tween(game.Workspace.CameraLakeA, game.Workspace.CameraLakeB)
	wait(1)
	while Player.PlayerGui.HomeScreen.Fade.Transparency < 1 do
		Player.PlayerGui.HomeScreen.Fade.Transparency = Player.PlayerGui.HomeScreen.Fade.Transparency + .05
		wait(.05)
	end
	wait(42)
	while Player.PlayerGui.HomeScreen.Fade.Transparency > 0 do
		Player.PlayerGui.HomeScreen.Fade.Transparency = Player.PlayerGui.HomeScreen.Fade.Transparency - .05
		wait(.05)
	end
	tweenInfo = TweenInfo.new(
		15,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	tween(game.Workspace.CameraFossilA, game.Workspace.CameraFossilB)
	wait(1)
	while Player.PlayerGui.HomeScreen.Fade.Transparency < 1 do
		Player.PlayerGui.HomeScreen.Fade.Transparency = Player.PlayerGui.HomeScreen.Fade.Transparency + .05
		wait(.05)
	end
end

I tried destroying the script and even adding while do to the waits yet the script doesn't seem to stop.
1 Like

Try disabling the script, I guess?

To end the loop add a break inside the while loop.

Edit: Replied to the wrong person, sorry!

It’s all fine, mate :slight_smile:

For a while loop to be running, some condition must be being met. In your script for example, the primary while loop (which everything else is enclosed inside) will continue to run for as long as Player.Play.Value is equal to false. In order to stop it you could use break somewhere inside the loop to end it, or Player.Play.Value could be set to something other than false.

If the value is changed, doesn’t the loop run all the code inside until it goes back to check for it again. Or is there a way to make it constantly check for that value and stop the loop when that value is met?

Um You can use break as way to stop it or a value as @TyPlays_YT said

1 Like

Yes, it does.

I have not been able to find any way of remotely ending a function immediately upon a value being changed.

You could place several if statements inside the loop to check the value and use break if the value has been changed. However given the lengthy, wait(n)s, there could still be some delay before the loop would end.

Unfortunately I haven’t been able to find a direct solution. Perhaps you could try exploring alternative methods to what you are trying to achieve. For example, Player.Play.Value could be checked again before any tweening is carried out, and you could could set the GUI’s Visible property to false upon Player.Play.Value being changed. That way the player should be able to continue with the game even though the loop is still finishing off the last few lines of code. This is just a thought.

I wish you all the best on finding a solution.

Two ways use the break function or disable the script

You can use break at any point needed.

To end a while loop, you can use the break function.

Thanks for the help. I’ve gotten to the point of stopping the loop and all I have to do is make it so the camera is put back to the character immediately after…

Use the break function, or make the Player.Play.Value == true, as it will loop as long as that value is false.

The Solution: Don’t use While wait() do, use For count 1, --amount of time-- do