Custom animation doesn't play

I am scripting a monster with a custom model, I created a walk and attack animation for him but both aren’t working in-game, I checked and the ids are correct, and I am not getting a single error message

local NPC = script.Parent
local HumanoidRootPart = NPC.HumanoidRootPart
local Humanoid = NPC.Humanoid

local MaxDistance = 350
local debounce = false

local animator = NPC.AnimationController.Animator

local anims = script.Parent.animations
local walkAnim = anims.walkAnimation
local slashAnim = anims.slashAnimation

local loadedWalkAnim = animator:LoadAnimation(walkAnim)
local loadedSlashAnim = animator:LoadAnimation(slashAnim)

loadedWalkAnim.Priority = Enum.AnimationPriority.Action2
loadedSlashAnim.Priority = Enum.AnimationPriority.Action3

NPC.Humanoid.Touched:Connect(function(hit) -- damage player
	if hit.Parent:FindFirstChild("Humanoid") and not debounce then
		debounce = true
		loadedSlashAnim:Play()
		hit.Parent.Humanoid.Health -= 20
		wait(1)
		debounce = false
	end
end)

while wait() do
	local Players = game.Players:GetPlayers()
	local closest

	for i, plr in pairs(Players) do
		if plr.Character and plr.Character:FindFirstChild("Humanoid") and plr.Character.Humanoid.Health > 0 then
			local PlayerHumanoidRootPart = plr.Character.HumanoidRootPart
			local Distance = (HumanoidRootPart.Position - PlayerHumanoidRootPart.Position).Magnitude

			if not closest then
				closest = PlayerHumanoidRootPart
			end

			if (HumanoidRootPart.Position - closest.Position).Magnitude > Distance then
				closest = PlayerHumanoidRootPart
			end


		end
	end

	if closest and (HumanoidRootPart.Position - closest.Position).Magnitude <= MaxDistance then -- move to closest player
		NPC.Humanoid:MoveTo(closest.Position)
	end
	
	Humanoid.Running:Connect(function(speed)
		if speed > 0 then
			if not loadedWalkAnim.IsPlaying then
				print("walk anim is playing")
				loadedWalkAnim:Play()
			end
		else
			if loadedWalkAnim.IsPlaying then
				print("walk anim is stopping")
				loadedWalkAnim:Stop()
			end
		end
	end)	
end

5 Likes

Are your print lines printing showing the animation is being played?
Is this a local or servers script? Animations for players are played locally and then sent to the server.

Try testing using the Start button to see if the animation is playing differently according to if you are looking at the server or the local views.

4 Likes

I am not getting any print lines but even when I move the play anim line under the NPC.Humanoid:MoveTo(closest.Position) the animation still wont play

2 Likes

Did you check the 2nd and 3rd things I mentioned, or only look for the prints?

(Not really related to the issue but when scripting use task.wait() instead of wait() because it’s more efficient. wait() has been deprecated)

So if your prints aren’t printing you need to find out why.

	Humanoid.Running:Connect(function(speed)
        print("speed = ", speed)  -- it may just be as simple as speed is being read as nil
		if speed > 0 then
			if not loadedWalkAnim.IsPlaying then
				print("walk anim is playing")
				loadedWalkAnim:Play()
			end
		else
			if loadedWalkAnim.IsPlaying then
				print("walk anim is stopping")
				loadedWalkAnim:Stop()
			end
		end
	end)	

It’s possible that moving the play anim line to the spot you mentioned is trying to start the animation every frame and it’s not allowing it to play fully.

2 Likes

that was a piece of code I added to see weather that will work but it didn’t so I just deleted it now, I changed where the animation is getting played to here

	if closest and (HumanoidRootPart.Position - closest.Position).Magnitude <= MaxDistance then -- move to closest player
		NPC.Humanoid:MoveTo(closest.Position)
		if not loadedWalkAnim.IsPlaying then
			print("running animation")
			print(loadedWalkAnim.IsPlaying)
			loadedWalkAnim:Play()
			print(loadedWalkAnim.IsPlaying)
		end	
	end
end

funny enough it says the animation is playing

4 Likes

You still haven’t replied about these.

1 Like

ah sorry, didn’t notice it, it’s in a server script

I figured out that it doesn’t play because it has a humanoid, if I delete that it works just fine

1 Like

Just remember to mark your post as the Solution so others can learn from it.

still need the humanoid to script the npc tho