I’m making a sprint with stamina system and I want to make an animation i made just play whenever I’m sprinting but no matter what I try it doesn’t play. The script is in StarterCharacterScripts which I’m pretty sure is where you put it.
--Services
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
--Variables
local LocalPlayer = game.Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local PlayerGui = LocalPlayer.PlayerGui
--Configurations
local Stamina = 100
local MaxStamina = 100
local RegainStamina = 5
local SprintSpeed = 20
local StaminaCost = 10
local NormalWalkSpeed = 16
--Booleans
local IsSprintHeld = false
local IsSprinting = false
local IsExhausted = false
--Animation
local RunAnim = game.ReplicatedStorage.Animations:FindFirstChild("RunAnim")
local Animator = Humanoid:FindFirstChildOfClass("Animator")
local SprintTrack = Animator:LoadAnimation(RunAnim) -- Load animation once
local function sprint(sprinting)
if IsExhausted then return end
IsSprinting = sprinting
Humanoid.WalkSpeed = sprinting and SprintSpeed or NormalWalkSpeed
end
local function OnSprintInput(input, gpe)
if input.KeyCode == Enum.KeyCode.LeftShift then
if input.UserInputState == Enum.UserInputState.Begin then
SprintTrack:Play()
IsSprintHeld = true
sprint(true)
elseif input.UserInputState == Enum.UserInputState.End then
SprintTrack:Stop()
IsSprintHeld = false
sprint(false)
end
end
end
local function updateStaminaUI()
PlayerGui.ScreenGui.StaminaBar.Bar.Size = UDim2.new(math.clamp(Stamina / MaxStamina, 0, 0.96), 0, 0.6, 0)
PlayerGui.ScreenGui.StaminaBar.Bar.Bar.Text = tostring(math.floor(Stamina)) .. "/" .. tostring(math.floor(MaxStamina))
--Change the path to match your gui path and change the 0.96 and 0.6 to the size of your bar (0.96 as the X value and 0.6 as the Y value)
--If you want it as a percent just change the "/" to a "%" and remove the .. tostring(math.floor(MaxStamina))
--If you want just bar with no numbers just remove the entire line that has the tostring(math.floor(Stamina))
end
UserInputService.InputBegan:Connect(OnSprintInput)
UserInputService.InputEnded:Connect(OnSprintInput)
RunService.Heartbeat:Connect(function(deltaTime)
if IsSprinting then
Stamina = math.max(0, Stamina - StaminaCost * deltaTime)
updateStaminaUI()
if Stamina == 0 then
sprint(false)
IsExhausted = true
end
else
Stamina = math.min(MaxStamina, Stamina + RegainStamina * deltaTime)
updateStaminaUI()
if Stamina >= MaxStamina * 0.2 then
IsExhausted = false
if IsSprintHeld then
sprint(true)
end
end
end
end)