My npc Ai makes a lap when turn in a corner

im trying to make a AI npc but i got this problem, when the npc turn in a corner the character do a full lap and it makes lost time to the npc and it looks ugly and i don’t know how to fix that

my code here:

local npc = script.Parent.Parent
local humanoid = npc.Humanoid
local npchrp = npc.HumanoidRootPart


local pfindservice = game:GetService("PathfindingService")

while wait() do
	--------------------------
	function showpath(path)
		local waypoints = path:GetWaypoints()
		for _, waypoint in ipairs(waypoints) do
			local part = Instance.new("Part", workspace)
			part.Shape = Enum.PartType.Ball
			part.Material = Enum.Material.Neon
			part.Anchored = true
			part.CanCollide = false
			part.Size = Vector3.new(1,1,1)
			part.Position = waypoint.Position
			game:GetService("Debris"):AddItem(part, 10)
		end
	end
	--------------------------
	
	local puntos = workspace.waypoints:GetChildren()
	local randomun = math.random(1,#puntos)
	
	local pathParams = {
		["AgentHeight"] = 4,
		["AgentRadius"] = 2,
		["AgentCanJump"] = true,
		Costs = {
			Indetect = math.huge
		}
	}

	local path = pfindservice:CreatePath(pathParams)

	path:ComputeAsync(npc.HumanoidRootPart.Position, puntos[randomun].Position)
	
	if path.Status == Enum.PathStatus.Success then
		local wayp = path:GetWaypoints()
		for i, waypoints in pairs(wayp) do
			---------------------
			local seepoints = showpath(path)
			---------------------
			if waypoints.Action == Enum.PathWaypointAction.Jump then
				humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
				
			humanoid:MoveTo(waypoints.Position)
			humanoid.MoveToFinished:Wait()
		end
	end
end

image
It seems as though there are 2 waypoints here so the NPC goes to it twice.

1 Like

I just saw that it’s not just for that one, how could I fix it?

I think it’s because the ai overshoots that waypoint.
So instead of doing humanoid.MoveToFinished:Wait() you can repeatedly check if the ai is within a certain distance. And then have it continue to the next waypoint.

1 Like