Pathfinding AI trying to walk trough the wall instead of get around it

Hi. I’m trying to make an AI for a game, but i dont know why the AI doesn’t work.
AI tries to walk trough the wall instead of getting around it and find a player.
Here is the code:

local pathfinding = game:GetService("PathfindingService")
local players = game:GetService("Players")
local runservice = game:GetService("RunService")

local enemy = script.Parent
local humanoid = enemy:WaitForChild("Humanoid")
enemy.PrimaryPart:SetNetworkOwner(nil)

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

local walkspeed = enemy:GetAttribute("WalkSpeed")
local sprintspeed = enemy:GetAttribute("SprintSpeed")

local function getPath(desination)
	local path = pathfinding:CreatePath({
		AgentHeight = 10;
		AgentRadius = 3;
		AgentCanJump = false;
		AgentCanClimb = false;
		
		Costs = {
			Water = 100;
		}
	})
	
	path:ComputeAsync(enemy:WaitForChild("HumanoidRootPart").Position, desination.Position)
	
	return path
end

local function canSeeTarget(target)
	local NPCtoCharacter = (target.Head.Position - enemy.Head.Position).Unit
	local npcLook = enemy.Head.CFrame.LookVector
	local dotProduct = NPCtoCharacter:Dot(npcLook)
	local origin = enemy.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - enemy.HumanoidRootPart.Position).Unit * 100
	local ray = Ray.new(origin, direction)
	
	local hit, pos = workspace:FindPartOnRay(ray, enemy)
	print(dotProduct)
	
	if dotProduct > -0.1 then
		if hit then
			if hit:IsDescendantOf(target) then
				return true
			end
		end
	else
		return false
	end
end

local function findTarget()
	local nearestTarget
	local maxDistance = 100
	
	for i,v in pairs(players:GetPlayers()) do
		if v.Character then
			local target = v.Character
			local distance = (enemy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			--and canSeeTarget(target)
			
			if distance < maxDistance then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end
	
	return nearestTarget
end

local function Capture(target)
	local distance = (enemy:WaitForChild("HumanoidRootPart").Position - target.HumanoidRootPart.Position).Magnitude
	
	if distance > 3 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		target.Humanoid:TakeDamage(10)
	end
end

local function WalkTo(desination)
	local path = getPath(desination)
	
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()
			
			if target then
				Capture(target)
				enemy.Humanoid.WalkSpeed = sprintspeed
				break
			else
				enemy.Humanoid.WalkSpeed = walkspeed
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		humanoid:MoveTo(desination.Position - (enemy.HumanoidRootPart.CFrame.LookVector * 10))
	end
end

local function Patrol()
	local waypoints = script.Parent.Parent.Waypoints:GetChildren()
	local randomWaypoint = math.random(1, #waypoints)
	WalkTo(waypoints[randomWaypoint])
end

wait(5)

runservice.Heartbeat:Connect(function()
	Patrol()
end)

Thanks for support!

Tell me what this prints:

local pathfinding = game:GetService("PathfindingService")
local players = game:GetService("Players")
local runservice = game:GetService("RunService")

local enemy = script.Parent
local humanoid = enemy:WaitForChild("Humanoid")
enemy.PrimaryPart:SetNetworkOwner(nil)

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

local walkspeed = enemy:GetAttribute("WalkSpeed")
local sprintspeed = enemy:GetAttribute("SprintSpeed")

local function getPath(desination)
	local path = pathfinding:CreatePath({
		AgentHeight = 10;
		AgentRadius = 3;
		AgentCanJump = false;
		AgentCanClimb = false;

		Costs = {
			Water = 100;
		}
	})

	path:ComputeAsync(enemy:WaitForChild("HumanoidRootPart").Position, desination.Position)

	return path
end

local function canSeeTarget(target)
	local NPCtoCharacter = (target.Head.Position - enemy.Head.Position).Unit
	local npcLook = enemy.Head.CFrame.LookVector
	local dotProduct = NPCtoCharacter:Dot(npcLook)
	local origin = enemy.HumanoidRootPart.Position
	local direction = (target.HumanoidRootPart.Position - enemy.HumanoidRootPart.Position).Unit * 100
	local ray = Ray.new(origin, direction)

	local hit, pos = workspace:FindPartOnRay(ray, enemy)
	print(dotProduct)

	if dotProduct > -0.1 then
		if hit then
			if hit:IsDescendantOf(target) then
				return true
			end
		end
	else
		return false
	end
end

local function findTarget()
	local nearestTarget
	local maxDistance = 100

	for i,v in pairs(players:GetPlayers()) do
		if v.Character then
			local target = v.Character
			local distance = (enemy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

			--and canSeeTarget(target)

			if distance < maxDistance then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end

	return nearestTarget
end

local function Capture(target)
	local distance = (enemy:WaitForChild("HumanoidRootPart").Position - target.HumanoidRootPart.Position).Magnitude

	if distance > 3 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	else
		target.Humanoid:TakeDamage(10)
	end
end

local function WalkTo(desination)
	local path = getPath(desination)

	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			local target = findTarget()

			if target then
				Capture(target)
				enemy.Humanoid.WalkSpeed = sprintspeed
				break
			else
				enemy.Humanoid.WalkSpeed = walkspeed
				humanoid:MoveTo(waypoint.Position)
				humanoid.MoveToFinished:Wait()
			end
		end
	else
		print("Unable to find path")
		humanoid:MoveTo(desination.Position - (enemy.HumanoidRootPart.CFrame.LookVector * 10))
	end
end

local function Patrol()
	local waypoints = script.Parent.Parent.Waypoints:GetChildren()
	local randomWaypoint = math.random(1, #waypoints)
	WalkTo(waypoints[randomWaypoint])
end

task.wait(5)

while task.wait() do
	Patrol()
end
1 Like

Nothing. This is not an error, it’s a bug. I cant fix it

By the way, where is destination defined, maybe im just blind lol

Destination is the object, that position will be used

1 Like