--services
local CAS = game:GetService("ContextActionService")
local RNS = game:GetService("RunService")
--settings
local staminausage =.2
local rechargetime = 1
--script
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
--animation
local sprint = script.sprintanim
local sprintAnim = humanoid.Animator:LoadAnimation(sprint)
local lastSprint = math.huge
local isSprinting = false
local function sprint(actionName, inputState, inputType)
if inputState == Enum.UserInputState.Begin then
isSprinting = true
elseif inputState == Enum.UserInputState.End then
isSprinting = false
end
if isSprinting then
humanoid.WalkSpeed = humanoid:GetAttribute("SprintSpeed")
sprintAnim:Play(.15)
else
humanoid.WalkSpeed = humanoid:GetAttribute("BaseWalkSpeed")
sprintAnim:Stop(.25)
end
end
CAS:BindAction("Sprint", sprint, true, Enum.KeyCode.LeftShift)
RNS.RenderStepped:Connect(function(delta)
local stamina = humanoid:GetAttribute("Stamina")
if stamina < staminausage then
isSprinting = false
humanoid.WalkSpeed = humanoid:GetAttribute("BaseWalkSpeed")
sprintAnim:Stop(.25)
end
if isSprinting and humanoid.MoveDirection.Magnitude > 0 then
humanoid:SetAttribute("Stamina", stamina - staminausage)
lastSprint = tick ()
else
if tick() - lastSprint >= rechargetime and stamina < humanoid:GetAttribute("MaxStamina") then
humanoid:SetAttribute("Stamina", stamina + .1)
end
end
if humanoid.MoveDirection.Magnitude <= 0 then
sprintAnim:Stop(.25)
end
if humanoid.MoveDirection.Magnitude == 0 and stamina < humanoid:GetAttribute("MaxStamina") then
humanoid:SetAttribute("Stamina", stamina + .15)
end
print(stamina)
end)
The issue with the animation not resuming after stopping movement or pressing shift before movement is because of the conditions under which the sprint animation is being played or stopped.
-- services
local CAS = game:GetService("ContextActionService")
local RNS = game:GetService("RunService")
-- settings
local staminaUsage = .2
local rechargeTime = 1
-- script
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
-- animation
local sprint = script.sprintanim
local sprintAnim = humanoid.Animator:LoadAnimation(sprint)
local lastSprint = math.huge
local isSprinting = false
local function sprint(actionName, inputState, inputType)
if inputState == Enum.UserInputState.Begin then
isSprinting = true
elseif inputState == Enum.UserInputState.End then
isSprinting = false
end
if isSprinting and humanoid.MoveDirection.Magnitude > 0 then
humanoid.WalkSpeed = humanoid:GetAttribute("SprintSpeed")
if not sprintAnim.IsPlaying then
sprintAnim:Play(.15)
end
else
humanoid.WalkSpeed = humanoid:GetAttribute("BaseWalkSpeed")
sprintAnim:Stop(.25)
end
end
CAS:BindAction("Sprint", sprint, true, Enum.KeyCode.LeftShift)
RNS.RenderStepped:Connect(function(delta)
local stamina = humanoid:GetAttribute("Stamina")
if stamina < staminaUsage then
isSprinting = false
humanoid.WalkSpeed = humanoid:GetAttribute("BaseWalkSpeed")
if sprintAnim.IsPlaying then
sprintAnim:Stop(.25)
end
end
if isSprinting and humanoid.MoveDirection.Magnitude > 0 then
humanoid:SetAttribute("Stamina", stamina - staminaUsage)
lastSprint = tick()
else
if tick() - lastSprint >= rechargeTime and stamina < humanoid:GetAttribute("MaxStamina") then
humanoid:SetAttribute("Stamina", stamina + 0.1)
end
end
if humanoid.MoveDirection.Magnitude == 0 then
if sprintAnim.IsPlaying then
sprintAnim:Stop(.25)
end
if stamina < humanoid:GetAttribute("MaxStamina") then
humanoid:SetAttribute("Stamina", stamina + 0.15)
end
end
print(stamina)
end)
See if this fixes your issue, let me know if there’s any questions.