Rather than constantly checking for whether an argument is true or not in a piece of code before running the next line, I want to know if it's possible to terminate a specific piece of code outside of that code, i.e. using a loop on a coroutine to check whether a specific argument is true and terminate it that way. I know break terminates a piece of code, but it can't do it remotely as I'd like to. I know that deleting a script ends the code running on the script, but I'd rather not cause extra lag with all of the instances I'd have to be using, since I'm planning on using local scripts.
If you'd like to see what I'm trying to accomplish with this, I'll post it in a code block down below. Basically, I'm trying to make a sprint script that changes your FOV and speed once you hold down or let go of shift. The problem here is the transition: If you stop sprinting while your FOV is tweening and visa-versa, then it will screw up the script. I have managed to fix this so that the transition tweens smoothly, but I have to use debounce in four areas and cutting coroutines would've been much simpler.
The following code is on a local script, located in StarterGui
local player = game.Players.LocalPlayer local character = player.Character local mouse = player:GetMouse() local camera = game.workspace.CurrentCamera local debounce = true mouse.keyDown:connect(function(key) if key:byte() == 48 and debounce then debounce = false if camera.FieldOfView < 100 then for i = camera.FieldOfView, 100, 2 do if not debounce then camera.FieldOfView = i character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed + i*2/3/2 wait() else return end end camera.FieldOfView = 100 character.Humanoid.WalkSpeed = 26 end end end) mouse.KeyUp:connect(function(key) if key:byte() == 48 and not debounce then debounce = true if camera.FieldOfView > 70 then for i = camera.FieldOfView, 70, - 2 do if debounce then camera.FieldOfView = i character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed + i*2/3/2 wait() else return end end camera.FieldOfView = 70 character.Humanoid.WalkSpeed = 16 end end end)