Pathfinding Issues

So, I am making a bigfoot type horror game. Hes supposed to go after the player when in range, and stop when not in range. He freezes after he kills a player, or when in a cave he just runs into the wall. I have no idea what could be causing this.

local ztor = script.Parent.HumanoidRootPart
local zhum = script.Parent.Humanoid
local function FindTar()
	local agro = 150

	local vector3 = Vector3.new(150,150,150)
	local magnitude = vector3.Magnitude
	local target
	for i, v in pairs(workspace:GetChildren()) do
		local hum = v:FindFirstChild("Humanoid")
		local tor = v:FindFirstChild("HumanoidRootPart")
		if hum and tor and v ~= script.Parent then
			if v:FindFirstChild("HumanoidRootPart"):FindFirstChild("Foot") then
			else
				if (ztor.Position - tor.Position).Magnitude < agro then
					agro = (ztor.Position - tor.Position).Magnitude
					target = tor
				end		
			end			
		end
	end
	return target
end
while wait(0.25) do 
	local tor = FindTar()
	if tor then
		print("sense")
		script.Parent.Humanoid.WalkSpeed = 35
		local finish = tor
		local dummy = script.Parent
		local humaoid = dummy.Humanoid
		local PathfindingService = game:GetService("PathfindingService")
		while true do
			print("Found")
			local path = PathfindingService:CreatePath()
			path:ComputeAsync(dummy.HumanoidRootPart.Position, tor.Position)
			local waypoints = path:GetWaypoints()
			for i = 1, 5 do
				if i <= #waypoints then
					if waypoints[i].Action == Enum.PathWaypointAction.Jump then
						humaoid:ChangeState(Enum.HumanoidStateType.Jumping)
					end			
					humaoid:MoveTo(waypoints[i].Position)
					humaoid.MoveToFinished:Wait()
					local function setNetworkOwnerOfBasePart(basePart, networkOwner)
						local success, errorReason = basePart:CanSetNetworkOwnership()
						if success then
							basePart:SetNetworkOwner(networkOwner)
						else
							error(errorReason)
						end
					end
					local function setNetworkOwnerOfModel(model, networkOwner)
						for _, descendant in pairs(model:GetDescendants()) do
							if descendant:IsA("BasePart") then
								setNetworkOwnerOfBasePart(descendant, networkOwner)
							end
						end
					end
					local function setNetworkOwner(instance, networkOwner)
						if instance:IsA("Model") then
							setNetworkOwnerOfModel(instance, networkOwner)
						elseif instance:IsA("BasePart") then
							setNetworkOwnerOfBasePart(instance, networkOwner)
						else
							warn(instance.Name .. "'s network ownership cannot be modified.")
						end
					end
					setNetworkOwner(script.Parent)
				end
			end
		end
	else
		script.Parent.Humanoid.WalkSpeed = 15
	end
end

Does the NPC run into the wall towards the player?

No, it does not. It runs to the left always if it spawns in the cave.

Try printing out tor before you return it. Then, click on it in the output.

It prints out what its supposed to, the path is just reporting the position wrong.

image
Added parts for the waypoints positions, and bigfoot is in like a back and forth motion for eternity.

That’s because the number 5 in for i = 1, 5 do is odd. An even number should allow it to work.

Hes stopped moving bath and forth, but now when faced with walls he just runs into them and doesnt stop. Im thinking its because the world is made of terrain.

Yeah, terrain could be an issue. Does changing AgentRadius help?

It works better, but he still ends up just running into a wall.

I got it working by making the walls of the terrain steeper and making rock a no-go for stepping on