Sprint button with cooldown

I want to make a sprint button (ContextActionService) with a cooldown. I want to add a cooldown in button’s content showing how much time is left to sprint again. Also when ContextAction button is clicked it holds the sprint forever like a radiobutton, however I want to make it so that when player is holding the button, he sprints. Any help is appreciated!

-- this is just sprint
local function sprint()

	sprinting = not sprinting
	if sprinting then
		character.Humanoid.WalkSpeed = Configuration.RunSpeed
		TweenService:Create(camera, TweenInfo.new(Configuration.TweenFovDuration, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = Configuration.RunFov}):Play()

	else
		character.Humanoid.WalkSpeed = Configuration.WalkSpeed
		TweenService:Create(camera, TweenInfo.new(Configuration.TweenFovDuration, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {FieldOfView = Configuration.WalkFov}):Play()

	end
	
end
2 Likes

Assuming you want the cooldown to start after they stop sprinting you could do something like this:

local sprintCooldown = 0

if sprinting then

  if sprintCooldown == 0 then
    -- sprint
  end

else
-- stop sprinting

  if sprintCooldown == 0 then
    sprintCooldown = 5
    repeat task.wait(1); sprintCooldown -= 1 until sprintCooldown == 0
  
  end

end
2 Likes

Or you could use a true / fase denounce but it gets the same job don3

But how do I find out that Context Action Service button is held down?

Could you send a SS of the workspace

I do not really have anything except run with shift for pc users. And I already solved it with gui button

Try using debounce.
Heres a small (but not related to sprinting sort-of) example.
This is only a quick script and may not work… It simply changes the player who clicked while holding a tool. It will set the humanoid’s WalkSpeed to 50 and they cant use the tool again after 0.8 seconds. After, the tool makes the player speed back to 16.

local debounce = false
local Tool = script.Parent
local CoolDownTime = 0.8

Tool.Activated:Connect(function(player)
	if not debounce then
		player.Character.Humanoid.WalkSpeed = 50
		debounce = true
		task.wait(CoolDownTime) -- if this doesnt work just replace CoolDownTime to something like 0.8. If it doesnt work as-well, just remove "task." at the beginning.
		player.Character.Humanoid.WalkSpeed = 16
		debounce = false
end

I didn’t test the script and only made it here, so I doubt it will work, but it might give you an idea how to make the cooldown actually work in your script.
Hope you figure it out.