Custom rigged NPC's animate script doesn't work

This is the animate script I found and I don’t know why it doesn’t work. I tried replacing RunService.Stepped:Connect(move) with RunService.Heartbeat:Connect(move) but I got the same result.

local RunService = game:GetService("RunService")

local character = script.Parent
local humanoid = character:WaitForChild("Enemy")

local walkAnim = script:WaitForChild("Walk")
local walkAnimTrack = humanoid.Animator:LoadAnimation(walkAnim)
walkAnimTrack.Priority = Enum.AnimationPriority.Movement

local idleAnim = script:WaitForChild("Idle")
local idleAnimTrack = humanoid.Animator:LoadAnimation(idleAnim)
idleAnimTrack.Priority = Enum.AnimationPriority.Idle

local function move()

	if humanoid.MoveDirection.Magnitude > 0 then
		if not walkAnimTrack.IsPlaying and idleAnimTrack.IsPlaying then
			idleAnimTrack:Stop()
			walkAnimTrack:Play()
		end
	else
		if walkAnimTrack.IsPlaying and not idleAnimTrack.IsPlaying then
			idleAnimTrack:Play()
			walkAnimTrack:Stop()
		end
	end
end

RunService.Stepped:Connect(move)

Thank you for helping

The MoveDirection property on humanoid is only changed when the player has control of the character. Any NPC would not change that value, as it would just stay at (0, 0, 0).

You have to get the velocity by calculating it manually.

local RunService = game:GetService("RunService")

local character = script.Parent
local humanoidRootPart = character:WaitForChild("HumanoidRootPart");
local humanoid = character:WaitForChild("Enemy")

local walkAnim = script:WaitForChild("Walk")
local walkAnimTrack = humanoid.Animator:LoadAnimation(walkAnim)
walkAnimTrack.Priority = Enum.AnimationPriority.Movement

local idleAnim = script:WaitForChild("Idle")
local idleAnimTrack = humanoid.Animator:LoadAnimation(idleAnim)
idleAnimTrack.Priority = Enum.AnimationPriority.Idle

local function move()
    local startPos = humanoidRootPart.Position;
    local startTick = tick();

    RunService.Heartbeat:Wait();

    local nowPos = humanoidRootPart.Position;

    local velocity = (nowPos - startPos).Magnitude / (tick() - startTick);

	if (velocity > 0) then
		if not walkAnimTrack.IsPlaying and idleAnimTrack.IsPlaying then
			idleAnimTrack:Stop()
			walkAnimTrack:Play()
		end
	else
		if walkAnimTrack.IsPlaying and not idleAnimTrack.IsPlaying then
			idleAnimTrack:Play()
			walkAnimTrack:Stop()
		end
	end
end

RunService.Stepped:Connect(move)
1 Like

Still doesn’t work unfortunately

Can you show us a screenshot of the hierarchy in explorer? We don’t know what “Walk” and “Idle” is, or how the humanoid is set up with the character.

Your code and hierarchy looks fine.

Can you confirm that your animation is running at all by removing the .Stepped function and move() and just straight up play the walk and idle animations.

local RunService = game:GetService("RunService")

local character = script.Parent
local humanoid = character:WaitForChild("Enemy")

local walkAnim = script:WaitForChild("Walk")
local walkAnimTrack = humanoid.Animator:LoadAnimation(walkAnim)
walkAnimTrack.Priority = Enum.AnimationPriority.Movement

local idleAnim = script:WaitForChild("Idle")
local idleAnimTrack = humanoid.Animator:LoadAnimation(idleAnim)
idleAnimTrack.Priority = Enum.AnimationPriority.Idle

idleAnimTrack:Play()
wait(5)
idleAnimTrack:Stop()

walkAnimTrack:Play()
wait(5)
walkAnimTrack:Stop()