I'm trying to make an npc that follows the closest player. I have everything working (to my knowledge...) however the npc is a bit robotic in that it does not have smooth transitions between waypoints. I'm trying to figure out why that is. (i've ruled out lag since there are other npc's that move smoothly)
Here is a gif of whats happening https://gyazo.com/dbce5cbb8e78ab6a8b38c799dd77a68b
Since I'm not sure exactly what the problem is, here is the full script
local zombie = workspace.Zombie
local humanoid = zombie.Humanoid
local pathFindingService = game:GetService("PathfindingService")
local wayPointsIndex
local maxDistance = 200--detection range
local wayPoints = {}
local currentPoint
function findtarget()
local closestDistance
local closestPlayer
for i,v in pairs(game.Players:GetPlayers())do
if v.Character then
local distance = (v.Character.HumanoidRootPart.Position-zombie.Torso.Position).magnitude
if not closestDistance then closestDistance = distance end
if distance <= closestDistance and distance < maxDistance then
closestDistance = distance
closestPlayer = v
end
end
end
return closestPlayer
end
function createPath()
local player = findtarget()
if player then
local character = player.Character
local path = pathFindingService:CreatePath()
path:ComputeAsync(zombie.Torso.Position,character.HumanoidRootPart.Position)
local wayPoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for i,v in pairs(wayPoints)do
if v.Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
humanoid:MoveTo(v.Position)
humanoid.MoveToFinished:Wait(2)
end
end
end
end
while true do
createPath()
wait()
end
Any help is appreciated :)

1 comment
I had the same problem. but I noticed in a server npc pathing movement is smoother compared to in the studio.