How can I make a no holding keybind?

Hello, I’m a beginner at scripting and I was making a sprint system with UserInputService.

So, when I holding the sprint key, the sprint works fine but now i want the sprint works when I press the key only once and when I press it again, the sprint stops.

I was looking for some help in videos and the webside but I couldn’t find the solution I want to archieve.

Here is the part from the LocalScript that I made for the sprint key:

userInputService.InputBegan:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.V and not processed then

		if humanoid.MoveDirection.Magnitude <= 0 then
			
			running.Value = true
            
			humanoid.WalkSpeed = sprintSpeed
			tween_fov(0.5, normalFov)
			
		else
			
			running.Value = true
            
			humanoid.WalkSpeed = sprintSpeed
			tween_fov(0.5, sprintFov)
			sprintTrack:Play(0.25)
			
		end
    end
end)

userInputService.InputEnded:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.V and not processed then

		running.Value = false
        
        humanoid.WalkSpeed = normalSpeed
		sprintTrack:Stop(0.25)
		
		Remote:FireServer()
		
    end
end)

I would like to know what is the problem and the solution. I was trying to use debounces but I don’t know how to use it or where to put it…

You want a toggle.

Runmode = false

Then when the key is pressed
Runmode = not Runmode

I suppose you are going to need a constant check of where the input is as well, so you have a constant MoveTo direction

So what should be the changes in the script using toggle?

I’m not versed in these input events (I use a single button for everything), but you could start with something like this. You only need the click (which might not be best with inputbegan, I dunno)

I need more caffeine, but I can’t see any issues yet:

local runmode = false
userInputService.InputBegan:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.V and not processed then
      runmode = not runmode --toggle
      if runmode then --running
		if humanoid.MoveDirection.Magnitude <= 0 then
			running.Value = true
			humanoid.WalkSpeed = sprintSpeed
			tween_fov(0.5, normalFov)
		else
			running.Value = true
			humanoid.WalkSpeed = sprintSpeed
			tween_fov(0.5, sprintFov)
			sprintTrack:Play(0.25)
		end
      else --not running
	    if input.KeyCode == Enum.KeyCode.V and not processed then
	  	    running.Value = false
            humanoid.WalkSpeed = normalSpeed
	    	sprintTrack:Stop(0.25)
	    	Remote:FireServer()
        end
    end
end)

userInputService.InputEnded:Connect(function(input, processed)
end)
1 Like

Thanks! that was I’m looking for :grinning_face_with_smiling_eyes: