Change players run animation while moving?

Basically in the game when the player stats sprinting I change their running animation. But this doesn’t actually apply until I stop moving and start moving again. I have tried to stop all the active animations in the player after applying the new animation ID in hopes that it would force it to restart with the new one but it seems to do nothing. Here is the code which is in a server script(tried disabling anims locally too) that is in the player character:

local MS = script.Parent.CharInfo.MoveState
local WS = script.Parent.CharInfo.WeaponState
local WT = script.Parent.CharInfo.WeaponType
local Chr = script.Parent
local animateScript = Chr:WaitForChild("Animate")


MS.Changed:Connect(function() --MoveState Changed
local AnimationTracks = Chr.Humanoid:GetPlayingAnimationTracks()

for i, v in pairs (AnimationTracks) do
	v:Stop()
end
if MS.Value == "Walk" then --Player Walking
	if WS.Value == "Armed" then --Player Armed
		if WT.Value == "Rifle" then --Armed Rifle
			animateScript.run.RunAnim.AnimationId = "rbxassetid://4169434554"
			animateScript.walk.WalkAnim.AnimationId = "rbxassetid://4169434554"
		elseif WT.Value == "Pistol" then --Armed Pistol
			
		end
	elseif WS.Value == "UnArmed" then --PLayer unarmed
		
	end
elseif MS.Value == "Sprint" then --Player Sprinting
	if WS.Value == "Armed" then --Player Armed
		if WT.Value == "Rifle" then --Armed Rifle
			animateScript.run.RunAnim.AnimationId = "rbxassetid://4169584019"
			animateScript.walk.WalkAnim.AnimationId = "rbxassetid://4169584019"
		elseif WT.Value == "Pistol" then --Armed Pistol
			
		end
	elseif WS.Value == "UnArmed" then --Player Unarmed
		
	end
end
end)

Sorry seems like a stupid problem but I can’t seem to get around it. I have looked through tons of forums and the GetPLayingAnimationTracks() seems to be the solution but it does nothing for me doesn’t even stop the running animation.

4 Likes

Have you changed the animation priority of the Sprint and Walking? Another thing is that how are the variables changing, are you using UIS or something else?

In addition, I’d also suggest using an Animation Instance, loading that into the player, and then playing it to see if it helps.

3 Likes

I know this is an old post, but I ran across it after experiencing the exact same problem: I am trying to change the ‘run’ animation, mid-run, but found that the animation will only change if the player stops running, even for a split second.

I’m new to all of this, so my solutions are generally very low tech. I ended up handling this by forcing a very short pause in the running by setting the walk speed to zero and back quickly. It does produce the slightest of stutters, but works well to force the animation change mid-run.

I have these few lines within my larger functioned triggered when I want the animation(s) changed:

		character.Animate.run.RunAnim.AnimationId = ("rbxassetid://04827147149")
		character.Animate.walk.WalkAnim.AnimationId = ("rbxassetid://04827121322")
		humanoid.WalkSpeed = 0
		wait(0.01)
		humanoid.WalkSpeed = player.Speed.Value
1 Like

Sadly, changing animation ID’s to swap between walking - running animations is impossible and requires hacky workarounds (like you provided). You have to load a custom animation into the humanoid and play it whenever the player starts running to do it properly.
The issue is that you need to make a lot of logistic checks for character running, jumping, being blocked, etc. to make sure that the running animation is fluid - playing, stopping and replaying the animation at specific times and events.

So if you’re looking for a simple run animation, just do
local Animation = Humanoid:LoadAnimation(RunningAnimation)
then
Animation:Play()
whenever you want to run, and
Animation:Stop()
to stop the running animation. I’ll leave the logistics up to you.

3 Likes

Elaborating on this, Humanoid:GetPropertyChangedSignal(“MoveDirection”) is a great event to use when determining if the player is running or not, Humanoid.MoveDirection is a Vector3 that shows you the direction relative to the world (with magnitude of 1 when moving, and 0 when not) which means if it’s anything but Vector3.new(0,0,0) then the player is moving/running. if you have sprinting you can do Root.Velocity or WalkSpeed checks as well. finally for ground checking humanoid.FloorMaterial can be used, anything other than Enum.Material.Air means you’re on the ground.

3 Likes

I ran into this issue as well and found a pretty simple solution:

  1. Play the new animation manually. (The current animation will continue playing “underneath” the new one)

  2. Listen for the Stopped event on the current animation track.

  3. Stop the new track when that event fires.

     for _, currTrack in pairs(humanoid:GetPlayingAnimationTracks()) do
         if currTrack.Name == "RunAnim" then
             print("is running")
    
             local newAnim = Instance.new("Animation")
             newAnim.AnimationId = ANIM_SWORD_RUN
             
             local newTrack = humanoid:LoadAnimation(newAnim)
             newTrack.Looped = true
             newTrack:Play()
    
             currTrack.Stopped:Connect(function()
                 print("stopped running")
                 newTrack:Stop()
             end)
         end
     end
    

This way you don’t have to look at WalkSpeed, MoveDirection, FloorMaterial, or anything of that sort to determine when the new animation should stop.

5 Likes

Just a late suggestion, It would work perfectly if you could somehow fuse your script with the default animate script, found in the player. this would make playing animations and switching them out really smooth, Sadly I don’t know how to do that YET. And will be experimenting with this in the future -.-

1 Like

Here’s one possible solution. https://devforum.roblox.com/t/how-can-i-swap-out-the-walking-animations-ingame/1729659
I’ve used this too and it works fine as long as humanoid’s walk speed changes when you toggle between run and walk.

Hi, the thread is from 2019, remember to check the date :smiley:

You could try playing both animations and simply changing the animation speed when you change stat. For instance when walking, walkinganim:AdjustSpeed(1). Then for when sprinting is true do walkinganim:AdjustSpeed(0), sprintanim:AdjustSpeed(1).