What is the best way to move an NPC to a specific point?

npcClone.Humanoid:MoveTo(CustomerPositions["1"].Position)
npcClone.Humanoid.MoveToFinished:Wait()
npcClone.Humanoid:MoveTo(CustomerPositions["2"].Position)
npcClone.Humanoid.MoveToFinished:Wait()
npcClone.Humanoid:MoveTo(CustomerPositions["3"].Position)
npcClone.Humanoid.MoveToFinished:Wait()
npcClone.Humanoid:MoveTo(chosenEnterPoint.Position)
npcClone.Humanoid.MoveToFinished:Wait()
npcClone.Humanoid:MoveTo(chosenShelvePoint.Position)
npcClone.Humanoid.MoveToFinished:Wait()

I have this piece of code to move an NPC from a specific start point to an endpoint and there are exactly 5 points that the NPC should follow, but I don’t think this is the best way to do that. So, is there any way to do that better.

2 Likes
for i,point in ipairs(CustomerPositions) do
    npcclone.humanoid:MoveTo(point.Position)
    npcclone.humanoid.movetofinished:Wait()
end

That should work. Make sure to include ipairs so it’s ordered.

1 Like

Make sure to capitalize your ‘Humanoid’ and ‘npcClone’ as it wouldn’t work without that.

2 Likes

I just copied the code from his script, for some reason it decapitalised the words. :man_shrugging:

2 Likes

This is the way to go for sure.

If CustomerPositions is e.g. a Folder then OP will have to use GetChildren to actually get the waypoint parts. If they do that, they’ll have to sort them because GetChildren doesn’t necessarily return the children in any specific order.

function getWaypoints(): {Part}
    local ch = CustomerPositions:GetChildren()
    table.sort(ch, function(a, b)
        return tonumber(a) < tonumber(b)
    end)
    return ch
end

for _, waypoint: Part in ipairs(getWaypoints()) do
    npc.Humanoid:MoveTo(waypoint.Position)
    npc.Humanoid.MoveToFinished:Wait()
end

Oh and just a heads up, MoveTo has some really quirky and honestly bad behavior. If it takes more than 5 seconds it just gives up. Here’s a workaround if you need it:

1 Like