Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a NPC Follow you with a Custom Walking Animation?

Asked by
SpiralRBX 224 Moderation Voter
5 years ago
Edited 5 years ago

Follow Code:

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 50
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("Torso")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end

while true do
    wait(0.1)
    local target = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end
end

How do I alter this script to make the NPC chase you with a custom animation? So far it just chases you with NO animation.

--EDIT-- Animation Code

wait()
state = {Enum.HumanoidStateType.Running,
        Enum.HumanoidStateType.RunningNoPhysics,
        Enum.HumanoidStateType.Jumping,
        Enum.HumanoidStateType.Freefall,
        Enum.HumanoidStateType.Landed
}

-----------EDIT THIS PART--------------

animations = {"2718926162", -- Running/Walking
            "", -- Idle
            "", -- Moving Jump
            "", -- Idle Jump
            "", -- Freefalling
            "" -- Landing
 }

---------------------------------------
--Model
CurrentState = ""
Animation = ""
canPlay = true

Player = game.Players.LocalPlayer

function playAnimation(Source,animation,player)
    if canPlay then
        local player = script.Parent.Parent
        canPlay = false
        local animTrack = Source.Humanoid:LoadAnimation(animation) -- Load animation into Humanoid
        canPlay = true
        animTrack:Play() -- Start the animation
    end
end

local animation = Instance.new("Animation")

local character = Player.Character
local humanoid = character.Humanoid

humanoid.StateChanged:connect(function(old,new)
    if CurrentState == "Running" and new == state[3] then -- Moving to Jump
        CurrentState = "Jump"

        if animations[3] ~= "" then
            animation.AnimationId = "http://www.roblox.com/Asset?ID="..animations[3]
            playAnimation(character,animation)
        end
    end

    if CurrentState == "Idle" and new == state[3] then -- Idle to Jump
        CurrentState = "Jump"

        if animations[4] ~= "" then
            animation.AnimationId = "http://www.roblox.com/Asset?ID="..animations[4]
            playAnimation(character,animation)
        end
    end

    if new == state[4] then
        CurrentState = "Falling"

        if animations[5] ~= "" then
            animation.AnimationId = "http://www.roblox.com/Asset?ID="..animations[5]
            playAnimation(character,animation)
        end
    end

    if new == state[5] then
        CurrentState = "Landed"

        if animations[6] ~= "" then
            animation.AnimationId = "http://www.roblox.com/Asset?ID="..animations[6]
            playAnimation(character,animation)
        end
    end

end)

humanoid.Running:connect(function(state)
    if state > 3 then
        if CurrentState ~= "Running" then
            CurrentState = "Running"

            if animation[1] ~= "" then
                animation.AnimationId = "http://www.roblox.com/Asset?ID="..animations[1]
                playAnimation(character,animation)
            end
        end
    else
        if CurrentState ~= "Idle" then
            CurrentState = "Idle"

            if animation[2] ~= "" then
                animation.AnimationId = "http://www.roblox.com/Asset?ID="..animations[2]
                playAnimation(character,animation)
            end
        end
    end
end)

1 answer

Log in to vote
0
Answered by
SpiralRBX 224 Moderation Voter
5 years ago

I have rewritten the code and have got it all working!

Ad

Answer this question