AI Pathfinding broken

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to make an AI that follows moving targets using path finding service.

  1. What is the issue? Include screenshots / videos if possible!

The AI always stutters to follow me (Walks back and forth) and only follows me once I stop moving.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried many things, returning if it is recomputing or resetting path but it always keeps stuttering whenever it follows me.
I’ve been at this non-stop for some days and I am so frustrated over it.

Here’s my current code:

function module.Move(self,point)
	if self == module then error("SELF == MODULE | CALL FROM MODULE.NEW") return end
	local currentWaypointIndex
	self.Waypoints = {}
	local typeOfPoint
	typeOfPoint = typeof(point)
	local position
	local acceptedTypes = {
		["CFrame"] = true;
		["Vector3"] = true;
		["Instance"] = true;
	}
	if acceptedTypes[typeOfPoint] ~= nil then
		if typeOfPoint == "Vector3" then
			position = point
		elseif typeOfPoint == "CFrame" then
			position = point.Position
		elseif typeOfPoint == "Instance" then
			if point:IsA("Model") then
				if point.PrimaryPart == nil then error("MODEL MUST HAVE A PRIMARYPART.") return end
				position = point.PrimaryPart.Position
			elseif point:IsA("BasePart") or point:IsA("UnionOperation") then
				position = point.Position
			end
			self.Target = point
		end
	else
		error("POINT TO WALKTOWARDS IS NOT AN ACCEPTED TYPE | CFRAME, VECTOR3, PART, UNION, MODEL")
		return
	end
	
	local template = self.Template
	local humanoid = template:FindFirstChildOfClass("Humanoid")
	local hrp = template:FindFirstChild("HumanoidRootPart")
	
	local path = createPath(self,position)
	if self.ResettingPath then return end
	
	self.ActiveProxy = newproxy()
	local CurrentProxy = self.ActiveProxy
	
	if self.ActiveProxy ~= CurrentProxy then self.IsRunning = false return end
	if self.ActiveProxy == nil then return end
	
	local instance
	
	if point == "Instance" then
		if point:IsA("Model") then
			instance = point.PrimaryPart
		elseif point:IsA("BasePart") or point:IsA("UnionOperation") then
			instance = point
		end
	end
	
	if path.Status == Enum.PathStatus.Success then
		if self.ResettingPath then self.IsRunning = false return end
		if self.ActiveProxy ~= CurrentProxy then self.ActiveProxy = nil end
		if self.ActiveProxy == nil then return end
		self.Waypoints = path:GetWaypoints()
		currentWaypointIndex = 1
		humanoid:MoveTo(self.Waypoints[currentWaypointIndex].Position)
		self.IsRunning = true
	else
		print("How can I get over there?!")
	end
	
	humanoid.MoveToFinished:Connect(function(reached)
		if self.ResettingPath then self.IsRunning = false return end
		if self.ActiveProxy ~= CurrentProxy then self.ActiveProxy = nil end
		if self.ActiveProxy == nil then return end
		if reached and currentWaypointIndex < #self.Waypoints then
			currentWaypointIndex = currentWaypointIndex + 1
			humanoid:MoveTo(self.Waypoints[currentWaypointIndex].Position)
			self.IsRunning = true
		end
	end)
	
	path.Blocked:Connect(function(blockedWaypointIndex)
		if blockedWaypointIndex > currentWaypointIndex then
			humanoid:MoveTo(-template.PrimaryPart.LookVector*10)
			self.ResettingPath = true
			self:Move(self.Target)
		end
	end)
end

function createPath(self,destination)
	self.RecomputingPath = true
	self.Path:ComputeAsync(self.Template.PrimaryPart.Position,destination)
	self.RecomputingPath = false
	self.ResettingPath = false
	return self.Path
end
-- functionality
	while rS.Heartbeat:Wait() do
		if self.Target and (self.Target.HumanoidRootPart.CFrame.Position - HumanoidRootPart.CFrame.Position).Magnitude > chosenAggroRange then
			self.Target = nil
			self.LastTargetPosition = nil
			self.FrequencyTarget = nil
		end
		if not self.Target or self.FrequencyTarget ~= self.Target then
			self:FindNewTarget()
		end
		if self.Target and self.LastTargetPosition ~= self.Target.HumanoidRootPart.Position then
			self.ActiveProxy = nil
			self.ResettingPath = true
			self:Move(self.Target)
		end
		if not self.IsRunning and self.Target then
			self.ActiveProxy = nil
			self.ResettingPath = true
			self:Move(self.Target)
		end
		if self.Target then
			self.LastTargetPosition = self.Target.HumanoidRootPart.Position
			self.FrequencyTarget = self.Target
		end
	end
1 Like