Make an NPC play animatons when walking

Hey! I’m trying to make my NPC play animations when walking. Here is my script:

local zombie = workspace["Drooling Zombie"]
local humanoid = zombie.Humanoid
local zombieHumanoidRootPart = zombie.HumanoidRootPart

local function findTargets()
    local targets = {}
    for i,plr in ipairs(game:GetService("Players"):GetPlayers()) do
        local char = plr.Character or plr.CharacterAdded:Wait()
        local hrp = char.HumanoidRootPart
        local mag = (hrp.Position - zombieHumanoidRootPart.Position).magnitude
        table.insert(targets, #targets+1, {mag, char.Name})
    end
    return targets 
end

local function getNearestTarget(targets)
    local nearestMag = math.huge
    local nearestPlayer 
    
    for i,v in ipairs(targets) do 
        if v[1] < nearestMag then
            v[1] = nearestMag
        end
    end
    --Get the nearestPlayer
    for i,v in ipairs(targets) do
        if v[1] == nearestMag then
            nearestPlayer = v[2]
        end
    end
    return nearestPlayer, nearestMag
end

local function followTarget(humanoidRootPart)
    humanoid:MoveTo(humanoidRootPart.Position)
end

while true do
    wait(1)
    local targets = findTargets()
    local plr, mag = getNearestTarget(targets)
    local hrp = workspace:WaitForChild(plr):WaitForChild("HumanoidRootPart")
    followTarget(hrp)
end

Everything works fine, but how would I make it play animations? I’d have to do it after this part probably

    local hrp = workspace:WaitForChild(plr):WaitForChild("HumanoidRootPart")
    followTarget(hrp)
end

Thanks!

play the game, look for your character, and copy Animate script.
then create server script inside your NPC, and paste code you copied earlier.
if it not works, delete part where there are commands for emotes.

1 Like