Pathfinding NPC keeps walking into walls

I am trying to make a pathfinding npc, it can currently look through glass and can sense when it can see me, however the pathfinding part is off.

My issue: The NPC constantly walks into walls, even though my code made it avoid them, and when it somehow manages to get into the building I’m in, it keeps bumping into walls for some reason, even though there are no obstacles in his way to get me.

Code: local teddy = script.Parent
local humanoid = teddy.Humanoid
–teddy.PrimaryPart:SetNetworkOwner(nil)

local function canSeeTarget(target)
local origin = teddy.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - teddy.HumanoidRootPart.Position).unit * 200
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {
workspace[“Police Station”],
workspace.Doorer
}

local raycastresults = workspace:Raycast(origin, direction, params)

if raycastresults and raycastresults.Instance then
	if raycastresults.Instance:IsDescendantOf(target) then
		print("Yes")
		return true
	else
		return false
	end
end

end

local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 200
local nearestTarget

for index, player in pairs(players) do
	if player.Character then
		local target = player.Character
		local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

		if distance < maxDistance and canSeeTarget(target) then
			nearestTarget = target
			maxDistance = distance
		end
	end
end

return nearestTarget

end

local function getPath(destination)
local PathfindingService = game:GetService(“PathfindingService”)

local pathParams = {
	["AgentCanJump"] = true,
	Costs = {
		Door = 20
	}
}

local path = PathfindingService:CreatePath(pathParams)

path:ComputeAsync(teddy.HumanoidRootPart.Position, destination.Position)

return path

end

local function attack(target)
local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

if distance > 4 then
	humanoid:MoveTo(target.HumanoidRootPart.Position)
else
	target.Humanoid.Health = 0
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
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
		end
	end
else
	humanoid:MoveTo(destination.Position - (teddy.HumanoidRootPart.CFrame.LookVector * 10))
end

end

function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end

while wait(0.25) do
patrol()
end

(The code isn’t formatting for some reason)

1 Like
local teddy = script.Parent
local humanoid = teddy.Humanoid
–teddy.PrimaryPart:SetNetworkOwner(nil)

local function canSeeTarget(target)
local origin = teddy.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - teddy.HumanoidRootPart.Position).unit * 200
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = {
workspace[“Police Station”],
workspace.Doorer
}

local raycastresults = workspace:Raycast(origin, direction, params)

if raycastresults and raycastresults.Instance then
	if raycastresults.Instance:IsDescendantOf(target) then
		print("Yes")
		return true
	else
		return false
	end
end
end

local function findTarget()
local players = game.Players:GetPlayers()
local maxDistance = 200
local nearestTarget

for index, player in pairs(players) do
	if player.Character then
		local target = player.Character
		local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

		if distance < maxDistance and canSeeTarget(target) then
			nearestTarget = target
			maxDistance = distance
		end
	end
end

return nearestTarget
end

local function getPath(destination)
local PathfindingService = game:GetService(“PathfindingService”)

local pathParams = {
	["AgentCanJump"] = true,
	Costs = {
		Door = 20
	}
}

local path = PathfindingService:CreatePath(pathParams)

path:ComputeAsync(teddy.HumanoidRootPart.Position, destination.Position)

return path
end

local function attack(target)
local distance = (teddy.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

if distance > 4 then
	humanoid:MoveTo(target.HumanoidRootPart.Position)
else
	target.Humanoid.Health = 0
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
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
		end
	end
else
	humanoid:MoveTo(destination.Position - (teddy.HumanoidRootPart.CFrame.LookVector * 10))
end
end

function patrol()
local waypoints = workspace.waypoints:GetChildren()
local randomNum = math.random(1, #waypoints)
walkTo(waypoints[randomNum])
end

while wait(0.25) do
patrol()
end