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…
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)