Why doesn't this animation play?

Hey devs,

So I am making an enemy npc system, and everything but the animation works. So, the damaging works. (which is literally after the animation) It all goes to the right place, and there is no error. Here is my script:

local npc = script.Parent
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")
local attackBoundaries = workspace:WaitForChild("Areas"):WaitForChild("Level 7 - Thieves"):WaitForChild("AttackBoundaries")

local plrsHit = {}
local maxDistance = 35
local attackCooldown = 1
local canAttack = true

local damageSound = Instance.new("Sound")
damageSound.Parent = npc
damageSound.SoundId = "rbxassetid://159504677"

local damageAnimation = script:WaitForChild("Animation")
local animator = npc.Humanoid:FindFirstChildOfClass("Animator")
local damageAnimTrack = animator:LoadAnimation(damageAnimation)

npc.Humanoid.Touched:Connect(function(touch)
    if not canAttack then return end
    local player = game.Players:GetPlayerFromCharacter(touch.Parent)
    if player and not plrsHit[player] then
        plrsHit[player] = true
        canAttack = false
        
        local humanoid = touch.Parent:FindFirstChild("Humanoid")
        if humanoid then
            humanoid:TakeDamage(25)
            damageSound:Play()
            print("Playing damage animation")
            damageAnimTrack:Play()
            
            plrsHit[player] = false
        end
        
        task.wait(attackCooldown)
        canAttack = true
    end
end)

while wait() do
    local plrs = game.Players:GetPlayers()
    local closestHRP
    
    for i, plr in pairs(plrs) do
        if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") and plr.Character.Humanoid.Health > 0 then
            local hrp = plr.Character.HumanoidRootPart
            local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude
            
            if not closestHRP then closestHRP = hrp end
            
            if (hrpOfNPC.Position - closestHRP.Position).Magnitude > distanceBetween then
                closestHRP = hrp
            end
        end
    end
    
    if closestHRP then
        local npcPos = hrpOfNPC.Position
        local targetPos = closestHRP.Position
        local withinBoundaries = attackBoundaries.CFrame:PointToObjectSpace(targetPos).Magnitude <= attackBoundaries.Size.Magnitude / 2
        
        if withinBoundaries and (npcPos - targetPos).Magnitude <= maxDistance then
            npc.Humanoid:MoveTo(targetPos)
        end
    end
end

Thank you! :heart:

I still need support on this, please help me!

Servers dont do animations - clients do. You will need a RemoteEvent to tell all clients that this humanoid is doing this exact animation.

EDIT: message sent itself somehow when I was not finished

1 Like

But then I would have to make a remote event for every NPC I need. (which is about 40)

Servers do animations but it is delayed.

In RemoteEvents you can pass instances as parameters, so you really only need one.

1 Like

Can you give me an example piece of code? I’ll make the remote event right now under ReplicatedStorage → Events

Give me a sec, I’ll write some example code and explain it for you

1 Like

While he writes a script for you, you could check the animation id if its correct and actually uploaded, or the animation priority

2 Likes

Definitely uploaded by me.

Why would this be important? I don’t know much about animations.

The EnemyAnimator is a LocalScript and should be placed in StarterPlayerScripts.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local npc = script.Parent
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")
local attackBoundaries = workspace:WaitForChild("Areas"):WaitForChild("Level 7 - Thieves"):WaitForChild("AttackBoundaries")

local plrsHit = {}
local maxDistance = 35
local attackCooldown = 1
local canAttack = true

local damageSound = Instance.new("Sound")
damageSound.Parent = npc
damageSound.SoundId = "rbxassetid://159504677"

local damageAnimation = script:WaitForChild("Animation")
local animator = npc.Humanoid:FindFirstChildOfClass("Animator")
local damageAnimTrack = animator:LoadAnimation(damageAnimation)

local AnimateEvent: RemoteEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("EnemyAnimateEvent")

npc.Humanoid.Touched:Connect(function(touch)
	if not canAttack then return end
	local player = game.Players:GetPlayerFromCharacter(touch.Parent)
	if player and not plrsHit[player] then
		plrsHit[player] = true
		canAttack = false

		local humanoid = touch.Parent:FindFirstChild("Humanoid")
		if humanoid then
			humanoid:TakeDamage(25)
			damageSound:Play()
			print("Playing damage animation")
			--damageAnimTrack:Play()
			AnimateEvent:FireAllClients(humanoid, damageAnimation) -- sends a message to all connected clients with the humanoid and animation. this will be used to play the animation on the client.

			plrsHit[player] = false
		end

		task.wait(attackCooldown)
		canAttack = true
	end
end)
--[...]
-- EnemyAnimator.lua
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local AniamteEvent: RemoteEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("EnemyAnimateEvent")

AniamteEvent.OnClientEvent:Connect(function(humanoid: Humanoid, animation: Animation) --once we receive the humanoid and animation from the server, we load the animation and play it
	local animator = humanoid:FindFirstChildOfClass("Animator")
	animator:LoadAnimation(animation):Play()
end)
1 Like

If your dummy playing another animation, and u played another animation with low priority the animation will work but you won’t be able to see it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.