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

Why isn't the NPC following me?

Asked by 2 years ago
Edited 2 years ago
function findNearestTorso(pos)

    local dist = 2048

    local list = workspace:GetChildren()
    local torso = nil
    local temp = nil
    local human = nil
    local temp2 = nil

    if list ~= nil then
        for x = 1, #list do
        temp2 = list[x]

        if (temp2.className == "Model") and (temp2 ~= script.Parent) and (temp2:FindFirstChildOfClass("LocalScript")) then

            temp = temp2:findFirstChild("HumanoidRootPart")
            human = temp2:findFirstChildOfClass("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

end

while wait(1) do
    target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
    x = math.random(1, 10)
    if x > 2 then
        if target ~= nil then
            script.Parent.Humanoid:MoveTo(target.Position)
        end
    else
        script.Parent:findFirstChildOfClass("Humanoid"):MoveTo(script.Parent.HumanoidRootPart.Position + Vector3.new(math.random(-10, 10), script.Parent.HumanoidRootPart.Position.Y, math.random(-10, 10)))
    end
end

This is a script i got from a friend that makes it so the enemy follows the player but for some reason it doesn't work how do i fix this?

0
I assure you, you didn't get it from a friend lmao greatneil80 2647 — 2y
0
no, his friend is the toolbox @greatneil80 Mathilinium 112 — 2y
0
i already figured out how to fix it dylancrazy88 20 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

ok i fixed it myself you are supposed to unanchor the humanoidrootpart which is set to true when you insert :/

Ad

Answer this question