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

Movement Script doesn't move the NPC?

Asked by 6 years ago

In this script, it detects a player, then moves an NPC to it. Everything seems to be working, but the NPC doesn't move. What am I doing wrong?

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 10000
    local temp = nil
    local human = nil
    local temp2 = nil


    for x = 1, #list do

        temp2 = list[x]
        print(temp2)
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("HumanoidRootPart")
            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(1)
    local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position, target)
        print("moveto")
    end
end

Thanks!

1 answer

Log in to vote
0
Answered by
Galicate 106
6 years ago

Using standard pathfinding would work but would have some flaws. If you want to detect when a player joins a game a simple

[npcname].Humanoid:MoveTo(game.Players.[playername or localplayer])

but if you want it to move when the player gets a certain distance to it then a testfor a humanoid within a certain stud radius would work fine too.

Ad

Answer this question