Sprint System BUG

I made this sprint system with stamina but when I spam my E key a bunch of times it regenerates my stamina super fast, I haven’t scripted in a while so I’m a little rusty

local SPRINTING_SPEED = 20
local MAX_STAMINA = 100
local stamina = MAX_STAMINA
local isSprinting = false

local function sprint(name, inputState)
	if inputState == Enum.UserInputState.Begin then
		isSprinting = true
		if isSprinting and stamina > 0 then
			humanoid.WalkSpeed = SPRINTING_SPEED
			repeat
				stamina -= 1
				print(stamina)
				task.wait(.1)
			until stamina <= 0 or not isSprinting
			humanoid.WalkSpeed = 16
		end
	elseif inputState == Enum.UserInputState.End then
		isSprinting = false
		if not isSprinting and stamina < MAX_STAMINA then
			repeat
				stamina += 1
				print(stamina)
				task.wait(.5)
			until stamina >= MAX_STAMINA or isSprinting
		end
	end
end

ContextActionService:BindAction("Sprint", sprint, false, Enum.KeyCode.E)

Possible make a Regen cool down. Im guessing it keeps running the Regen part really fast.

RocketSlither has a good point. You don’t recover instantly after being winded. So a cooldown fits well.

until stamina >= MAX_STAMINA or isSprinting
task.wait(5)

(something like that, didn’t test anything)

Add a debounce variable to prevent the function from executing too frequently.

1 Like

like this?

local SPRINTING_SPEED = 20
local MAX_STAMINA = 100
local stamina = MAX_STAMINA
local isSprinting = false
local isCoolingDown = false

local function sprint(name, inputState)
	if inputState == Enum.UserInputState.Begin then
		isSprinting = true
		if isSprinting and stamina > 0 then
			humanoid.WalkSpeed = SPRINTING_SPEED
			repeat
				stamina -= 1
				print(stamina)
				task.wait(.1)
			until stamina <= 0 or not isSprinting
			humanoid.WalkSpeed = 16
		end
	elseif inputState == Enum.UserInputState.End then
		isSprinting = false
		if not isSprinting and not isCoolingDown and stamina < MAX_STAMINA then
			isCoolingDown = true
			task.wait(3)
			isCoolingDown = false
			repeat
				stamina += 1
				print(stamina)
				task.wait(.5)
			until stamina >= MAX_STAMINA or isSprinting
		end
	end
end

ContextActionService:BindAction("Sprint", sprint, false, Enum.KeyCode.E)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.