Pathfinding monster walks into walls?

Pathfinding monster walks into walls?

Hey, there I’ve spent a lot of time recently trying to make a monster that patrols and whenever a player goes too close to the monster it will chase the player while avoiding obstacles.

The monster is patrolling just the way I want to but whenever a player reaches its radius of 80 studs it chases the player without avoiding obstacles. I hope somebody can help me with this.

Here is the script:

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
1 Like

The problem is that when it gets a path, it moves to the target instead of following the path.

local function Hunt(destination)
	
	local chase = Chase(destination)
	
	if chase.Status == Enum.PathStatus.Success then
	humanoid:MoveTo(destination.HumanoidRootPart.Position) -- this is the problem
	humanoid.MoveToFinished:Wait()
	else
		print("pathfinding failed")
	end
end

Instead, do this:

local function Hunt(destination)
	
	local chase = Chase(destination)
	
	if chase.Status == Enum.PathStatus.Success then
	for _,waypoint in pairs(chase:GetWaypoints()) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
	else
		humanoid:MoveTo(destination.HumanoidRootPart.Position) -- if it can't find a path, move to the target
	humanoid.MoveToFinished:Wait()
	end
end