Sprint animation stops correctly on client, but not on server

I’ve appended a function to the bottom of the default animation script, so that when a player begins sprinting the walk animation changes to a running animation. However, after the player stops moving, the walking/running animation continues to play on the server, but functions correctly on the client. This only occurs after the player has sprinted once; if the player has never sprinted before, it works normally.
Here is the code I have appended to the bottom of the ‘Animate’ script:

script.Parent.SprintActivate.OnClientEvent:Connect(function(value)
	if value == true then
		animNames.walk[1].id = "http://www.roblox.com/asset/?id=18257837863"
		script.walk.WalkAnim.AnimationId = "rbxassetid://18257837863"

		if script.Parent.Humanoid:GetState() == Enum.HumanoidStateType.Running then
			script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Landed)
		end
	else
		animNames.walk[1].id = "http://www.roblox.com/asset/?id=127301488311753"
		script.walk.WalkAnim.AnimationId = "rbxassetid://127301488311753"
		if script.Parent.Humanoid:GetState() == Enum.HumanoidStateType.Running then
			script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.Landed)
		end
	end
end)

(This code is before the while loop at the very bottom, so no, that’s not the reason.)

Originally, I had it manually begin playing the running animation, but that returned the same result.
Any help is much appreciated.

1 Like

If this script is running on client-side, then you have to use remoteEvents/UnreliableRemoteEvents, since what’s done on client, doesn’t replicate to server-side.

1 Like

This script is part of the default Animate script, which is present in all Roblox games and is by default a localscript. It does replicate to the server, however it seems to play both the walk and sprint animations at the same time when the player stops moving.

You don’t need to change the default animations script. Just make your own LocalScript in the character to handle the sprinting animation. You just have to ensure that your animation has priority over default animations.

1 Like

I know I could do that, but I’d prefer not to unless it’s absolutely necessary. I’m trying to organise my code better, so I’d like to handle animations in the Animate script. Is there any other way to make my current method work?

In my opinion, the way you’re doing it is less organised. I’d just recommend what I suggested.