Bot pathfinding code review

Hey all, I am working on working on a bot module for my game that handle core bot processes such as pathing, attacking and vision. I need some insight on how I can improve the pathfinding aspect of the bot module as I feel it could use some improving.

CoreNPC - Pathfinding

function NPC:MoveToLocation(location:Vector3)	
	local success, errormsg
	local RETRIES = 0
	local MAX_RETRIES = 5
	local RETRY_COOLDOWN = 0.5

	local waypoints
	local nextWaypoint

	repeat
		success, errormsg = pcall(function() self.Path:ComputeAsync(self.Torso.Position, location) end)
		RETRIES += 1
		task.wait(RETRY_COOLDOWN)
	until success or RETRIES >= MAX_RETRIES

	if success and self.Path.Status == Enum.PathStatus.Success and #self.Path:GetWaypoints() > 0 then
		waypoints = self.Path:GetWaypoints()

		for i, waypoint:PathWaypoint in ipairs(waypoints) do
			if i == 1 then continue; end
			nextWaypoint = i
			if waypoint.Action == Enum.PathWaypointAction.Jump then self.Humanoid.Jump = true; end
			self.Humanoid:MoveTo(waypoint.Position)
			local reached = self.Humanoid.MoveToFinished:Wait(5)
			if not reached then
				self:MoveToLocation(location)
			end
		end
		
		local connection; connection = self.Path.Blocked:Connect(function(index)
			if index >= nextWaypoint then
				connection:Disconnect()
				self:MoveToLocation(location)
			end
		end)
	end	
end

Its implementation lies in the main script

MainAI - Main

local function Main()
	local nearestTarget:BasePart = npc:FindTargets(exclusion)
	if nearestTarget then
		if npc:CheckSight(nearestTarget) then
			npc.Humanoid:MoveTo(nearestTarget.Position)
		else
			npc:MoveToLocation(nearestTarget.Position)
		end
		npc:Attack(nearestTarget)
	else
		npc.Humanoid:MoveTo(npc.Torso.Position)
	end
end

FYI this runs on a run service stepped loop

Problems that I have found:

  • The bot tends to jitter around turnings while walking using a path.
1 Like