Im trying to make an npc that will just walk behind the player and follow them around. I tried using pathfinding to do this but it seems to be really janky and chopy. Heres the code for it :
--Services
local PathFindingService = game:GetService("PathfindingService")
--Variables
local targetCharacter = script.Parent.Parent.Parent
local target = targetCharacter:WaitForChild("HumanoidRootPart")
local localCharacter = script.Parent.Parent
local localHumanoid = localCharacter:WaitForChild("Humanoid")
local localRoot = localCharacter:WaitForChild("HumanoidRootPart")
--Functions
function moveTo()
print("Moving to player")
local pos = target.Position + target.CFrame.LookVector * -2 + target.CFrame.RightVector * 2
localHumanoid:MoveTo(pos)
end
function drawWaypoint(point)
local part = Instance.new("Part", workspace)
part.BrickColor = BrickColor.new("White")
part.Shape = "Ball"
part.Material = "Neon"
part.Transparency = 0.25
part.Anchored = true
part.Locked = true
part.CanCollide = false
part.Size = Vector3.new(1,1,1)
part.Position = point.Position
game:GetService("Debris"):AddItem(part,1)
end
function generatePath()
print("Generating Path")
local path = PathFindingService:CreatePath()
path:ComputeAsync(localRoot.Position ,target.Position + target.CFrame.LookVector * -2 + target.CFrame.RightVector * 2)
return path
end
function moveAlongWaypoints(path)
print("Moving along waypoints")
local wayPoints = path:GetWaypoints()
for _,waypoint in pairs(wayPoints)do
print("Next waypoint")
drawWaypoint(waypoint)
localHumanoid:MoveTo(waypoint.Position)
localHumanoid.MoveToFinished:Wait()
end
end
function main()
local path = generatePath()
if path ~= nil and path.Status == Enum.PathStatus.Success then
print("Path was Successfully made")
moveAlongWaypoints(path)
else
warn("Failed to generate path")
end
end
--Loops main function
while true do
print("Running Main")
main()
game:GetService("RunService").Stepped:Wait()
end
If some one could fix the code to make the movement smoother or just tell me a better way I would apreaciate it alot :)

2 comments
Instead of doing while true do, use RenderStepped, because that will run everytime a new frame is generated. I think this would make the movements and turning smoother? But, I'm not sure. I don't really work with AI or PathFindingService. Fires every frame prior to the frame being rendered. https://developer.roblox.com/en-us/api-reference/event/RunService/RenderStepped
game:GetService("RunService"):Connect(function() print("Running Main") main() end)