#walk animation only works when player changes direction

1 messages · Page 1 of 1 (latest)

golden bolt
#

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local LocalPlayer = Players.LocalPlayer
local character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local camera = workspace.CurrentCamera

local head = character:WaitForChild("Head")
local sweat = head:FindFirstChild("SweatEmitter")

local walkSpeed = 10
local sprintSpeed = 25

local maxStamina = 100
local regenStamina = maxStamina / 600
local drainSpeed = 0.25
local stamina = maxStamina

character:SetAttribute("Maxstamina", maxStamina)
character:SetAttribute("stamina", stamina)

local sprint = false
local drained = false

local function getHumanoid()
local character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
return character:WaitForChild("Humanoid")
end

local humanoid = getHumanoid()
humanoid.WalkSpeed = walkSpeed

local runAnimation = script:WaitForChild("runAnimation")
local AnimTrack = humanoid:LoadAnimation(runAnimation)
AnimTrack.Looped = true
AnimTrack.Priority = Enum.AnimationPriority.Action

local walkAnimation = script:WaitForChild("walkAnimation")
local AnimTrack2 = humanoid:LoadAnimation(walkAnimation)
AnimTrack2.Looped = true
AnimTrack2.Priority = Enum.AnimationPriority.Idle

local function isMoving()
return humanoid.MoveDirection.Magnitude > 0
end

#

RunService.Heartbeat:Connect(function()
if sprint then
if stamina > 0 then
stamina = math.max(stamina - drainSpeed, 0)
character:SetAttribute("stamina", stamina)
else
sprint = false
drained = true
if AnimTrack.IsPlaying then
AnimTrack:Stop(0.1)
AnimTrack2:Play(0.1)
end
humanoid.WalkSpeed = walkSpeed
camera.FieldOfView = 60
sweat.Enabled = true
end
else
stamina = math.min(stamina + regenStamina, maxStamina)
character:SetAttribute("stamina", stamina)
if drained and stamina >= maxStamina then
drained = false
sweat.Enabled = false
end
if not isMoving() then
camera.FieldOfView = 60
end
end
end)

UserInputService.InputBegan:Connect(function(input, isTyping)
if isTyping then return end

if isMoving() then
    if input.KeyCode == Enum.KeyCode.LeftShift and not drained then
        sprint = true
        if not AnimTrack.IsPlaying then
            AnimTrack:Play(0.1) 
        end
        if AnimTrack2.IsPlaying then
            AnimTrack2:Stop(0.1) 
        end
        humanoid.WalkSpeed = sprintSpeed
        camera.FieldOfView = 55
    elseif not sprint then
        if not AnimTrack2.IsPlaying then
            AnimTrack2:Play(0.1)  
        end
    end
end

end)