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.
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: