Pahtfinding AI isn't chasing a player?

Hey there so I was working on a pathfinding ai that patrols around the map but whenever a player goes close to it the ai will run and attack the player that got near it. The script works perfectly fine but whenever a player goes near the ai the ai will not avoid obstacles while chasing the player.

local Wendigo = script.Parent
local humanoid = Wendigo.Humanoid
Wendigo.PrimaryPart:SetNetworkOwner(nil)
 
local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 80
	local nearestTarget = nil
 
	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (Wendigo.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
 
			if distance < maxDistance then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end
 
	return nearestTarget
end
 
local function Chase(target)
	local PathfindingService = game:GetService("PathfindingService")
 
	local pathParams = {
		["AgentHeight"] = 8,
		["AgentRadius"] = 2.2,
		["AgentCanJump"] = false
	}
 
	local path = PathfindingService:CreatePath(pathParams)
 
	path:ComputeAsync(Wendigo.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
 
	return path
end
 
local function getPath(destination)
	local PathfindingService = game:GetService("PathfindingService")
 
	local pathParams = {
		["AgentHeight"] = 8,
		["AgentRadius"] = 2.2,
		["AgentCanJump"] = false
	}
 
	local path = PathfindingService:CreatePath(pathParams)
 
	path:ComputeAsync(Wendigo.HumanoidRootPart.Position, destination.Position)
 
	return path
end
 
local function Hunt(destination)
 
	local chase = Chase(destination)
 
	if chase.Status == Enum.PathStatus.Success then
		humanoid:MoveTo(destination.HumanoidRootPart.Position)
		humanoid.MoveToFinished:Wait()
	else
		print("pathfinding failed")
	end
end
 
local function attack(target)
 
	local distance = (Wendigo.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
	if distance > 8 then
		print("chasing", target.name)		
		Hunt(target)
	else
		local attackAnim = humanoid:LoadAnimation(script.Attack)
		local scream = humanoid:LoadAnimation(script.ScreamAnim)		
		local screamsound = script.ScreamSound		
 
		Wendigo.HumanoidRootPart.Anchored = true		
		target.Humanoid.WalkSpeed = 0
		attackAnim:Play()
		task.wait(1)
 
		humanoid.WalkSpeed = 0
		target.Humanoid.Health = 0
 
		task.wait(1)
		screamsound:Play()
		scream:Play()
		wait(scream.Stopped)
		Wendigo.HumanoidRootPart.Anchored = false
	end
end
local function walkTo(destination)
 
	local path = getPath(destination)
 
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
			if target and target.Humanoid.Health > 0 then
				print("TARGET FOUND", target.Name)
				attack(target)
				break
			else
				print("Moving to ", waypoint.Position)
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
	
			end
		end
	end
end
 
function patrol()
	local waypoints = workspace.waypoints:GetChildren()
	local randomNum = math.random(1, #waypoints)
	walkTo(waypoints[randomNum])
end
 
while wait() do	
	patrol()
end

Thanks for reading.

1 Like

Well, what does the findTarget function return? You have no print’s, use that to debug stuff such as function’s.

You are returning a Path to the above function, but then not using the returned Path. Instead, you are moving straight to the target Humanoid. You need to be stepping through the returned waypoints as per normal pathifinding.

Also this:

local PathfindingService = game:GetService("PathfindingService")

Stop delcaring this constantly. Set it once at the beginning of the script, once and once only. You don’t need to keep repeating this as you will be using memory for each declared variable. Inefficiencies like that start to add up over all the scripts in a game.