Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Remotely terminating a code?

Asked by 9 years ago

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)
0
A hard question no doubt... you can terminate other functions inside of functions, in the case of this code I would put some booleans that dont allow you to stop sprinting and change the FOV at the same time, im not an expert at all so im going to let someone else answer this dragonkeeper467 453 — 9y
0
Yeah, I managed to do that with the debounce, but I think it would be several times more effective if I was able to use coroutines to remotely terminate the code. aquathorn321 858 — 9y

Answer this question