Cancelling UserInputBegan threads

Hi, I’m wondering how I can cancel a thread made by UserInputService.InputBegan when I call InputEnded. I looked everywhere and couldn’t find information about it.

Store the connection as a variable and then call :Disconnect() on it

I’d need to use local function for that right?

nah its legit just

local connection = UIS.InputBegan:Connect(function(key, gp)
    if not gp then 
        print(key.KeyCode)
    end
end)

local connection2 = nil

connection2 = UIS.InputEnded:Connect(function(key, gp)
    if not gp then 
        if connection then
            connection:Disconnect()
            connection = nil
            connection2:Disconnect() connection2 = nil
        end
    end
end

also a tip is if you want something to connect only once, You can use :Once() instead of :Connect()

One problem is that I can only use this function once. I want to stop the thread without actually deleting the function

What do you use to create the threads? task.spawn() or coroutine?

I’m pretty sure you can store functions into variables so you could probably do it that way?

– Input fuctions –

local slowDownTime
slowDownTime = UIS.InputBegan:Connect(function(inp, p)
if inp.KeyCode == Enum.KeyCode.LeftShift and not grounded and haveUsed1 == false then
for _, animation in pairs(Player.Character:FindFirstChildOfClass(“Humanoid”):GetPlayingAnimationTracks()) do
animation:AdjustSpeed(0.4)
end
task.wait(0.1)
GUI()
HeartBeat:Play()
HRP.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
HRP.Anchored = true
wait(0.01)
beingpressed = true
HRP.Anchored = false
HRP.AssemblyLinearVelocity = Vector3.new(0, 5, 0)
Humanoid.WalkSpeed = 8
game.Workspace.Gravity = 5
local ColorTint = game.Lighting.ColorCorrection
haveUsed1 = true
haveUsed = false
Camera(ColorTint, 90)
wait(1.4)
game.Workspace.Gravity = 150
ColorRevert(ColorTint, 90)
HeartBeat:Stop()
GUI2()
end
end)

UIS.InputEnded:Connect(function(inp, p)
if inp.KeyCode == Enum.KeyCode.LeftShift and not grounded and haveUsed == false then
if tooLong then return end
slowDownTime:Disconnect()
local lookVector = camera.CFrame.LookVector
local value = lookVector
beingpressed = false
HeartBeat:Stop()
haveUsed = true
Dash:Play()
wait(0.05)
GUI2()
game.Workspace.Gravity = 150
HRP.AssemblyLinearVelocity = Vector3.new(value.X210, value.Y100, value.Z*210)
local ColorTint = game.Lighting.ColorCorrection
ColorRevert(ColorTint, 130)
end
end)

Here is the script snippet

I tried it with both: I can only create coroutines in UserInputService otherwise I can only use it once

Could you add indentation and wrap it with `lua (no spaces) so I can read it properly?

Ugh use ` 3 times in a row together no space

sorry new to forums

-- Input fuctions --

local slowDownTime 
slowDownTime = UIS.InputBegan:Connect(function(inp, p)
	if inp.KeyCode == Enum.KeyCode.LeftShift and not grounded and haveUsed1 == false then	
		for _, animation in pairs(Player.Character:FindFirstChildOfClass("Humanoid"):GetPlayingAnimationTracks()) do
			animation:AdjustSpeed(0.4)
		end 
		task.wait(0.1)
		GUI()
		HeartBeat:Play()
		HRP.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
		HRP.Anchored = true
		wait(0.01)
		beingpressed = true
		HRP.Anchored = false
		HRP.AssemblyLinearVelocity = Vector3.new(0, 5, 0)
		Humanoid.WalkSpeed = 8
		game.Workspace.Gravity = 5
		local ColorTint = game.Lighting.ColorCorrection
		haveUsed1 = true
		haveUsed = false
		Camera(ColorTint, 90)
		wait(1.4)
		game.Workspace.Gravity = 150
		ColorRevert(ColorTint, 90)
		HeartBeat:Stop()
		GUI2()
	end
end)
	

UIS.InputEnded:Connect(function(inp, p)
	if inp.KeyCode == Enum.KeyCode.LeftShift and not grounded and haveUsed == false then
		if tooLong then return end
		slowDownTime:Disconnect()
		local lookVector = camera.CFrame.LookVector
		local value = lookVector
		beingpressed = false
		HeartBeat:Stop()
		haveUsed = true
		Dash:Play()
		wait(0.05)
		GUI2()
		game.Workspace.Gravity = 150
		HRP.AssemblyLinearVelocity = Vector3.new(value.X*210, value.Y*100, value.Z*210)
		local ColorTint = game.Lighting.ColorCorrection
		ColorRevert(ColorTint, 130)
	end
end)

I mean, you could also create a global variable

local enabled = True

and check if enabled is true before executing code.

and toggle the state of enabled depending on your needs.

I’ll try global variables

I just kind of want the thread to be cancelled completely when the player stops the input, and if the player doesn’t within 1.4s everything goes back to normal

Yeah, global variables are probably your solution

took me a while to figure it out but I finally did it.

I made a global variable, and then when the inputended I disconnected it, and at the end of the thread I just remade the variable.

kinda like this

_G.Slow = UIS.InputBegan:Connect(function(key, gp)
   --script
end)

UIS.InputEnded:Connect(function(key)
_G.Slow:Disconnect()
--script2
_G.Slow = UIS.InputBegan:Connect(function(key, gp)
   --script
end)
1 Like

Yep that works.
|character limit aaaaa|