I have a sprinting and stamina mechanic in my game. In order for your stamina to refill, you have to wait a cooldown of 2 seconds. However, holding down the sprint button then releasing it after your stamina hits 0 always resets the cooldown.
Example
My code:
local uis = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local tweenService = game:GetService("TweenService")
local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local hum = chr:WaitForChild("Humanoid")
-- events
local runEvent = game.ReplicatedStorage.runEvent
--animations
local sprintAnim = script.Animation
local Anim = hum:LoadAnimation(sprintAnim)
-- stamina settings
local stamina = 100 -- starting
local maxStamina = 100 -- what it will regen to
local drainRate = 1
local regenRate = 0.8
local staminaRegen = 20
local RefreshTime = 2
-- booleans
local tired = false
local sprinting = false
local sprintCd = false
local canRegen = false
-- guis
local frame = script.Parent.Frame
local staminaBar = frame:WaitForChild("FullStamina")
local UiStroke = frame:WaitForChild("UIStroke")
--sounds
local tiredsfx = script.Parent.tired
--tween for the frame going visible
local tweeninfo = TweenInfo.new(
1,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
0,
false,
0
)
-- Update bar UI
local function updateBar()
staminaBar:TweenSize(UDim2.new(stamina / maxStamina, 0, 1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 0)
end
local function regenCooldown()
if sprintCd then return end
sprintCd = true
canRegen = false
task.delay(RefreshTime, function()
if stamina < maxStamina then
canRegen = true
end
sprintCd = false
end)
end
uis.InputBegan:Connect(function(inp)
if inp.KeyCode == Enum.KeyCode.LeftShift then
runEvent:FireServer("sprint")
Anim:Play()
sprinting = true
-- tweens
local tweenFrame = tweenService:Create(frame, tweeninfo, {BackgroundTransparency = 0}):Play()
local tweenStamina = tweenService:Create(staminaBar, tweeninfo, {BackgroundTransparency = 0}):Play()
local tweenUiStroke = tweenService:Create(UiStroke, tweeninfo, {Transparency = 0}):Play()
end
end)
uis.InputEnded:Connect(function(inp)
if inp.KeyCode == Enum.KeyCode.LeftShift then
runEvent:FireServer("walk")
Anim:Stop()
sprinting = false
regenCooldown()
-- tweens
--local tweenFrameEnd = tweenService:Create(staminaBar, tweeninfo, {BackgroundTransparency = 1})
--local tweenStaminaEnd = tweenService:Create(staminaBar, tweeninfo, {BackgroundTransparency = 1})
--local tweenUiStrokeEnd = tweenService:Create(UiStroke, tweeninfo, {Transparency = 1})
--tweenFrameEnd:Play()
--tweenStaminaEnd:Play()
--tweenUiStrokeEnd:Play()
end
end)
-- sprint bar
runService.RenderStepped:Connect(function()
if sprinting and not tired then
stamina -= drainRate
if stamina <= 0 then
stamina = 0
sprinting = false
regenCooldown()
Anim:Stop()
runEvent:FireServer("walk")
print("tired")
tiredsfx:Play()
end
else
if not sprinting and canRegen and stamina < maxStamina then
stamina += regenRate
end
end
updateBar()
end)
How can I fix this?