Walk animation not updating

So I have a script that changes the walk speed and animation depending on the direction the character is facing. But the animation wouldn’t update until I stopped moving. For example I’m walking while holding W, then I switch to hold A while holding W, the animation is the the one for W not the for A. But if I stop holding W before switching A it would update. The walkspeed updates fine so I’m here to seek help, I am a new scripter and this my first time posting the devforum. Heres the script

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local character = script.Parent
local Humanoid = character:WaitForChild("Humanoid")
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
local runanim =script.Parent.Animate.walk.WalkAnim
-- Wait for PlayerScripts and necessary modules to load
local playerScripts = LocalPlayer:WaitForChild("PlayerScripts")
local playerModule = playerScripts:WaitForChild("PlayerModule")
local controlModule = require(playerModule):GetControls() -- Use GetControls() which returns the active ControlModule
while task.wait() do
	local moveVector = controlModule:GetMoveVector()
	if moveVector.Z < 0 then
		Humanoid.WalkSpeed = 16
		runanim.AnimationId = "rbxassetid://139317481490265"
	else 
		Humanoid.WalkSpeed = 10
		runanim.AnimationId = "rbxassetid://89491080922343"
	end
	
end


shouldn’t you stop the run animation, change the animation id, then play it?

thats what I thought but im not sure how to do that, since the animation is ran in another script. As I said I’m really new:(

you can try doing it anyway and seeing if it works, that’s how I normally code anyway; just trying stuff until it works.

basically, what I think we could try is playing the animation at a track priority higher than roblox’s default for running.

the code could look like this, maybe it works or doesn’t:

while task.wait() do
	local moveVector = controlModule:GetMoveVector()
	local animationId = ""
	if moveVector.Z < 0 then
		Humanoid.WalkSpeed = 16
		animationId = "rbxassetid://139317481490265"
	else 
		Humanoid.WalkSpeed = 10
		animationId = "rbxassetid://89491080922343"
	end
	
	if walkAnim.AnimationId ~= animationId then
		local newAnimation = Instance.new("Animation")
		newAnimation.Name = "WalkAnim"
		newAnimation.AnimationId = animationId
		newAnimation.Parent = walkAnim.Parent

		walkAnim:Destroy()
		walkAnim = newAnimation

		Humanoid:WaitForChild("Animator"):LoadAnimation(newAnimation):Play()
	end
end

Although the code doesn’t work, I’ll try out stuff and see if I can do anything myself.

is there a error that you could provide?

i had no error message, so I just removed the script cause. I think without the directional movement speed the game look better.