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


1 Like

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

1 Like

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.

should you close this as unresolved then

Could you try and use this function and store it in a local script in StarterPlayerScripts? This accounts for WASD movement so you can modify it there. I think this should work, I am not home so i cannot test it on Studio myself.

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")

local WalkAnimation = Instance.new("Animation")
WalkAnimation.AnimationId = "rbxassetid://your_walk_animation_id_here"

local CurrentAnimation = nil
local Speed = 16 -- Default Walkspeed for humanoids

local function UpdateMovementDirection()
    local MoveDirection = Humanoid.MoveDirection
    
    -- If The player is moving we run this code
    if MoveDirection.Magnitude > 0 then
        
        local angle = math.deg(math.atan2(MoveDirection.X, MoveDirection.Z))

        -
        if CurrentAnimation == nil or CurrentAnimation.IsPlaying == false then
            CurrentAnimation = Animator:LoadAnimation(WalkAnimation)
            CurrentAnimation:Play()
        end

        Humanoid.WalkSpeed = Speed
    else
        
        if CurrentAnimation then
            CurrentAnimation:Stop()
        end
    end
end

-- Kinda hacky way to check but it works :P
RunService.Heartbeat:Connect(function()
    UpdateMovementDirection()
end)

local function onInputChanged(input, gpe)
    if gpe then return end

    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D then
            if input.KeyCode == Enum.KeyCode.W then
                Speed = 16  -- going forward
            elseif input.KeyCode == Enum.KeyCode.A then
                Speed = 14  -- going left
            elseif input.KeyCode == Enum.KeyCode.D then
                Speed = 14  -- going right
            elseif input.KeyCode == Enum.KeyCode.S then
                Speed = 12  -- going backwards
            end
        end
    end
end

UserInputService.InputChanged:Connect(onInputChanged)

Is this what you wanted?

Script:

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

local character = script.Parent
local Humanoid = character:WaitForChild("Humanoid")
local runAnimTrack
local animateScript = character:WaitForChild("Animate")

local playerScripts = LocalPlayer:WaitForChild("PlayerScripts")
local playerModule = playerScripts:WaitForChild("PlayerModule")
local controlModule = require(playerModule):GetControls()

local function updateWalkAnim(animId)
	local walkAnim = animateScript.walk.WalkAnim
	if walkAnim.AnimationId ~= animId then
		walkAnim.AnimationId = animId

		if runAnimTrack then
			runAnimTrack:Stop()
		end
		
		runAnimTrack = Humanoid:LoadAnimation(walkAnim)
		if Humanoid.MoveDirection.Magnitude > 0 then
			runAnimTrack:Play()
		end
	end
end

while task.wait(0.1) do
	local moveVector = controlModule:GetMoveVector()
	if moveVector.Magnitude > 0 then
		if moveVector.Z < 0 then
			Humanoid.WalkSpeed = 16
			updateWalkAnim("rbxassetid://127944732487697")
		else
			Humanoid.WalkSpeed = 10
			updateWalkAnim("rbxassetid://86201914972577")
		end
	else
		if runAnimTrack then
			runAnimTrack:Stop()
		end
	end
end

Video: