Issue with sprinting script

Hello! So I’m working on a running system for my game which will change the animation and speed when the user presses control. There’s only one issue; if the user is still moving while the animation changes, the change wont happen until the user stops moving. My script is a server script in StarterCharacterScripts.

Script:

local Players = game:GetService("Players")
local myCharacter = script.Parent
local myPlayer = Players:GetPlayerFromCharacter(myCharacter)
local Humanoid = myCharacter.Humanoid

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local CD = 1
local OnCD = false

ReplicatedStorage.SprintActivated.OnServerEvent:Connect(function(Player)
	if OnCD == false then
	if Player ~= myPlayer then
		return
	end
		if myCharacter:FindFirstChild("Values").Sprinting.Value == false then
			myCharacter.Animate.walk.WalkAnim.AnimationId = "rbxassetid://96388853389416"
			Humanoid.WalkSpeed = 26
			myCharacter:FindFirstChild("Values").Sprinting.Value = true
			OnCD = true
			print("On "..Player.Name)
			task.delay(CD, function()
				OnCD = false
			end)
		else
			myCharacter.Animate.walk.WalkAnim.AnimationId = "rbxassetid://130277879283099"
			Humanoid.WalkSpeed = 9
			myCharacter:FindFirstChild("Values").Sprinting.Value = false
			OnCD = true
			print("Off "..Player.Name)
			task.delay(CD, function()
			OnCD = false
			end)
		end
	end
end)

Thank you as always ^^

Why would you want a serverscript? As far as I’m aware, most games make sprinting in a local script
(I can tell because when connection disconnects, I can still sprint on the client)

Im pretty sure you need to stop the current walking animation first and then play the new one almost instantly

local walkanim = myCharacter.Animate.walk.WalkAnim
local animator = myCharacter.Humanoid.Animator
for _,anim in ipairs(animator:GetPlayingAnimationTracks()) do
   if anim.Animation.AnimationId == walkanim.AnimationId then anim:Stop() end
end 
walkanim.AnimationId = -- The new id
animator:LoadAnimation(walkanim):Play() -- Initialisation of new walking anim

Now if i stop moving after pressing control the animation continues playing

EDIT: I messed with the animation script a little to stop the animation entirely if i stop moving, and now it works. Thanks!