Animation not working on NPC

Hi!

I’m having problems showing animations in my NPCs despite of trying several solutions taken from this forum. Please, let me detail my problem to see what I am missing.

I have created a very simple “hitting with a bat” animation for my player. It’s included inside a Bat tool and that Bat is inside my StarterPack. Each time the user clicks, the animation is played. This is the local script:

local bat = script.Parent
local replicatedStorage = game:GetService("ReplicatedStorage")
local hitEvent = replicatedStorage:FindFirstChild("hitEvent")
local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()
local debouncedMouse = true
local debouncedTouch = true

bat.Wood.Touched:Connect(function(hit)

	local character = hit.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	
	if humanoid and debouncedTouch then
		debouncedTouch = false
		hitEvent:FireServer(humanoid)
		task.wait(1)
		debouncedTouch = true
	end
end)

mouse.Button1Down:Connect(function()
	
	if debouncedMouse then
		debouncedMouse = false
		local animation = bat.battingAnimation
		local humanoid = bat.Parent:FindFirstChild("Humanoid")
		if humanoid then
			local animationTrack = humanoid:LoadAnimation(animation)	
			animationTrack.Priority = Enum.AnimationPriority.Action
			animationTrack.Looped = false
			animationTrack:Play()
			task.wait(1)
			animationTrack:Stop()
			animationTrack:Destroy()
		end
		debouncedMouse = true
	end
end)

This is working OK. Each time I click with my mouse, my player plays the animation, so perfect.

Now I would like my NPCs to be equipped with this bat, pursue the players making the hitting animation looped and walking animation. To do so I’ve made this:

  1. Copy/Paste the Bat from StarterPack into my NPCs into the workspace.
  2. Remove the local script inside the NPC.Bat.
  3. Create a new server script inside the NPC.Bat with this content:
local bat = script.Parent
local debounced = true

bat.Wood.Touched:Connect(function(hit)

	local character = hit.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	
	if humanoid and debounced then
		debounced = false
		humanoid:TakeDamage(20)
		task.wait(1)
		debounced = true
	end
end)
  1. Create a server script inside my NPC to pursue the nearest player with this content:
local npc = script.Parent
local pathfindingService = game:GetService("PathfindingService")
local npcHumanoid = npc:WaitForChild("Humanoid")
local npcRootPart = npc:WaitForChild("HumanoidRootPart")
local playersService = game:GetService("Players")
local MAX_DISTANCE = 25
local path = pathfindingService:CreatePath({
	AgentCanJump = false
})

function getNearestPlayer()

	local minDistance = math.huge
	local nearestPlayer
	local players = playersService:GetPlayers()
	for index, player in ipairs(players) do
		local character = player.Character or player.CharacterAdded:Wait()
		local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
		local distance = (humanoidRootPart.Position - npcRootPart.Position).magnitude
		if distance < minDistance then
			minDistance = distance
			nearestPlayer = player
		end
	end
	if minDistance < MAX_DISTANCE then
		return nearestPlayer
	end
end

function animateNpc()
	local animation = npc.Bat.battingAnimation
	local humanoid = npc.Humanoid

	if humanoid and animation then
		local animationTrack = humanoid:LoadAnimation(animation)	
		animationTrack.Priority = Enum.AnimationPriority.Movement
		animationTrack.Looped = true
		animationTrack:Play()
		task.wait(1)
		animationTrack:Stop()
		animationTrack:Destroy()
	end
end

function goTowardsPlayer(player)
	
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
	local startingPoint = npcRootPart.Position
	local endingPoint = humanoidRootPart.Position

	path:ComputeAsync(startingPoint, endingPoint)
	local waypoints = path:GetWaypoints()
	for i, waypoint in pairs(waypoints) do
		npcHumanoid:MoveTo(waypoint.Position)	
		npcHumanoid.MoveToFinished:Wait(1)
	end
end

while task.wait(1) do
	local nearestPlayer = getNearestPlayer()
	if nearestPlayer then
		animateNpc()
		goTowardsPlayer(nearestPlayer)
	end
end

With these changes, the NPC pursues the nearest player and the bat makes damage, so this is working OK (in fact, I’ve put lots of logs and they are all shown without problems and without any errors).
But, the NPC doesn’t play any animation, it slides instead of walking and it’s not hitting.

Regarding to the walking animation I’ve tried to use the Animate script that players have when they are playing copied inside the npc.humanoid but without success.

Regarding to the hitting animation I’ve tried to do use

local animator = humanoid:WaitForChild("Animator")
local animationTrack = animator:LoadAnimation(animation)	

instead of

local animationTrack = humanoid:LoadAnimation(animation)	

but I get this warning “Infinite yield possible on ‘Workspace.Adolescente cabreado.Humanoid:WaitForChild(“Animator”)’” and the animation is not played.

I’ve tried too using animationTrack.Priority = Enum.AnimationPriority.Movement and animationTrack.Priority = Enum.AnimationPriority.Action, removing the animation Stop and Destroy, removing the Looped=true, but always without success.

What could I try? Am I missing something?

1 Like

Try using an animation controller -

Make sure you own the animation, set priorty to action, make sure all parts are unanchored, when finding the animation make sure you use :WaitForChild(“battingAnimation”), make sure animation is looping, and make sure the if statement isn’t skipping over the code.

There can be many problem for animations not working, since they are quite funky and also you can place “animationNpc()” inside the function goTowardsPlayer, so it’ll play everytime it goes towards the player.

Thanks.
I’m trying again:

  • Putting the animateNpc() call inside in the goTowardsPlayer function.
  • Adding an AnimationController inside npc (in the workspace)
  • Changing the animateNpc() method to this:
function animateNpc()
	local animation = npc.Bat.battingAnimation
	local humanoid = npc.Humanoid
	local animationController = npc.AnimationController
	
	if humanoid and animation and animationController then
		
        print("humanoid, animation and animationControllers are ok")
		local animationTrack = animationController:LoadAnimation(animation)
		animationTrack.Priority = Enum.AnimationPriority.Action
		animationTrack:Play()
		task.wait(1)
		animationTrack:Stop()
		animationTrack:Destroy()
	end
end
  • log “humanoid, animation and animationControllers are ok” is always shown.
  • I’m the owner of the animation, in fact, this same animation works perfectly with my player in a local script.
  • I think all parts of my npc are unanchored, in fact, when npc dies, it makes the death animation breaking into pieces.

The npc keeps pursuing the player but none animation is played.

It might be because R15 animations don’t work on R6 Rig, vise versa.