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
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.
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.
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