I need a sprinting animation to trigger properly

I’m scripting a sprinting system for someone. The players that have access to the system have custom animations for idle, walk, and sprint animations. This means that I can’t just speed up the walk animation. I’m using ContextActionService for the sprinting, and I’ve come up with two solutions for how to play the animations properly, both of which don’t do exactly what I want.

  1. Changing out the default walk/run animations for the sprint one when the sprint key is pressed. This works perfectly when the player stops moving. They’ll keep their walk animation until they stop moving, then switch to the sprint animation as long as the sprint key is still pressed. Stopping the track that controls the walking animation so that the switch can take place doesn’t work.

  2. Mounting the animation on a custom track that will be set to play when the sprint key is held down. This solves the animation change problem, but it is arguably worse because the animation plays over everything (i.e. all idle, jump, fall, climb, etc. animations) while the sprint key is held down.

I’m at a loss to what to do at this point. I really don’t want to make a system that detects when the player is moving and plays the animation accordingly, so I need some advice on what to do.

Thanks in advance.

Use this, make it on a local script, have the default idle and walk changing by forking the animation script from the character and then copying it into starterCharacterScripts(Change the id’s of the walk and idle animation in the script). also if you don’t want sprinting running over things change the priority of the script or make the sprint stop running when another action plays.
Local script(In starterCharacterScripts)

local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(Input, GameProcess)
if GameProcess then -- this just make sures he ain't typing and it accidentally gets activated
if uis.Input == Enum.KeyCode. [[-- Whatever key you want sprint to be--]] then
humanoid.WalkSpeed = 24 -- The speed you want sprinting, also define humanoid yourself
Animation:Play() -- just have an animation object defined and load it then play it 
uis.InputEnded:Connect(function(Input, GameProcess)  -- When the key  is let go this will play
if uis.Input == Enum.KeyCode. [[-- Whatever key you want sprint to be--]] then 
humanoid.WalkSpeed = 16 -- Puts speed back
Animation:Stop() -- Stops the run animation

Thanks for your reply. If you could, can you go into more depth on how to make the animation stop running when another action plays? I’ve tried changing the animation priority but it doesn’t do anything until I get to Core, where it just plays once and then gets overrun by the other animations. I basically want it to behave like a walk animation would, except that switching out the walk animation with the sprint animation doesn’t play the animation until you stop moving.

Also, is there any reason to use UIS over CAS?

I don’t really know much about CAS but I believe that it’s a lot better to use UIS just because you have more control on what you can do, I don’t really understand what you mean by keeping it like the walk animation but what you would do is make your run animation priorty to action, then if you want to make the script cancel, add some if then statments for example if he is falling you would do something like this

if humanoid:GetState() == Enum.HumanoidStateType.Freefall then
-- stop the animation

Or you can change his stateType to be running(I believe you would have to use CAS for this) but you just make it so his stateType is running and this would allow animations to run like jumping when someone is sprinting

1 Like

Thank you for your help. I completely forgot that HumanoidState was a thing.