Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

sprint animation stop playing when i restart walking ,how can i fix?

Asked by
322ita 15
3 years ago

so i made this script and when i stop walking and then restart it play walk anim ,not sprint anim

local UserInputService = game:GetService("UserInputService")
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local sprintAnim = humanoid:LoadAnimation(script:WaitForChild("SprintAnim"))
local isSprinting = false

humanoid.Running:Connect(function(speed)
    if speed > 0 then
        sprintAnim:AdjustSpeed(1)
    else
        sprintAnim:Stop()
    end
end)

UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        if not isSprinting then
            isSprinting = true
            sprintAnim:Play()
            sprintAnim:AdjustSpeed(0)
            humanoid.WalkSpeed = 26
            humanoid.JumpPower = 50
        else
            sprintAnim:Stop()
            humanoid.WalkSpeed = 16
            humanoid.JumpPower = 50
            isSprinting = false 
        end 
    end
end)
0
u need to put a local script in startercharacterscript with a animation in it to test it 322ita 15 — 3y
0

2 answers

Log in to vote
0
Answered by
Elyzzia 1294 Moderation Voter
3 years ago
Edited 3 years ago

you need to play the animation whenever the character starts running, not just when the player presses shift

humanoid.Running:Connect(function(speed)
    if speed > 0 then
        if isSprinting and not sprintAnim.IsPlaying then
            sprintAnim:Play()
        end
        sprintAnim:AdjustSpeed(speed/26) -- pretty sure you'd want the animation's playback speed to change based on how fast the player is actually walking 
    else
        sprintAnim:Stop()
    end
end)

also, the player could press shift when they're standing still, so you'd need to check for that to make sure the sprinting animation doesn't play while they're not moving

UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        if not isSprinting then
            isSprinting = true
            if humanoid.MoveDirection.Magnitude > 0 then -- just checking the player's MoveDirection is the easiest way to tell if they're not pressing any movement keys
                sprintAnim:Play()
                sprintAnim:AdjustSpeed(0)
            end
            humanoid.WalkSpeed = 26
            humanoid.JumpPower = 50
        else
            sprintAnim:Stop()
            humanoid.WalkSpeed = 16
            humanoid.JumpPower = 50
            isSprinting = false 
        end 
    end
end)
0
thanks u helped me 322ita 15 — 3y
0
i just added a credit of you 322ita 15 — 3y
Ad
Log in to vote
0
Answered by
322ita 15
3 years ago

u need to put a local script in startercharacterscript with a animation in it to test it

Answer this question