Shifit to sprint animation randomly stopping

I have a shift to sprint script and I put an animation there but if I start running then it stops like 0.2 seconds after and if I turn around it also stops too. How do I fix this?

Script:

local Player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild(‘Humanoid’)

local RunAnimation = Instance.new(‘Animation’)
RunAnimation.AnimationId = ‘rbxassetid://8071883568’
RAnimation = Humanoid:LoadAnimation(RunAnimation)

Running = false

function Handler(BindName, InputState)
if InputState == Enum.UserInputState.Begin and BindName == ‘RunBind’ then
Running = true
Humanoid.WalkSpeed = 48
elseif InputState == Enum.UserInputState.End and BindName == ‘RunBind’ then
Running = false
if RAnimation.IsPlaying then
RAnimation:Stop()
end
Humanoid.WalkSpeed = 16
end
end

Humanoid.Running:connect(function(Speed)
if Speed >= 10 and Running and not RAnimation.IsPlaying then
RAnimation:Play()
Humanoid.WalkSpeed = 48
elseif Speed >= 10 and not Running and RAnimation.IsPlaying then
RAnimation:Stop()
Humanoid.WalkSpeed = 16
elseif Speed < 10 and RAnimation.IsPlaying then
RAnimation:Stop()
Humanoid.WalkSpeed = 16
end
end)

Humanoid.Changed:connect(function()
if Humanoid.Jump and RAnimation.IsPlaying then
RAnimation:Stop()
end
end)

game:GetService(‘ContextActionService’):BindAction(‘RunBind’, Handler, true, Enum.KeyCode.LeftShift)

Here is a little video about what I mean. Sorry for bad quality I had to compress it because the limit is 10mb.

I don’t think there’s anything necessarily wrong with your code. I think what’s happening is that once your run animations goes through a full cycle, it stops. This is because animations are meant to play once by default and not play again until triggered. You can combat this by simply setting the Looped value of your AnimationTrack to true, like this:

RAnimation.Looped = true -- line 8 (right after your RAnimation variable declaration)

Also, the way you’re retrieving the character from the character is a risky way of achieving this. What happens if a part in your workspace happens to have the same name as your player? I suggest you replace line 2 with the following code.

repeat wait() until Player.Character
local Character = Player.Character

Hope this helps. Let me know if you have any issues or further questions.

I did it and it didn’t work still the same thing.

Hmm. Could you send another video?

This is after I edited the script with the looping thing. It is the same thing.

Can’t you just manually change the Animation property to Looped?

I did that and it still does the same thing. I even changed it to looped in the animator.

Are you properly looping it in the Animator (the little triangle arrow with the arrow encircling it)?

Another option is that your Shift key isn’t working properly and is intermittent.

I did and that is what the animation is supposed to look like. But instead it starts for a tiny bit and ends right after like I showed on the other vids. yes the animation is a free model cuz i suck at animating but it was looped when i loaded it in.

What about the animation priority? Try setting that it to the “Action” value.

1 Like

wow it works i feel dumb for not noticing that. thanks

Let’s goo! Glad it’s working. Good luck on the rest of the project!