Help with Pathfinding

So my Cop NPC pathfinding is broken.
Problem:

local RunService = game:GetService("RunService")
local PathfindingService = game:GetService("PathfindingService")

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local WalkSpeed = humanoid.WalkSpeed

local DestinationsFolder = workspace:WaitForChild("Destinations")
character.PrimaryPart:SetNetworkOwner(nil)

local nextActivate = os.clock()
local debounceTime = 0.3
local combo = 1

local animationTable = {
	script:WaitForChild("Animation"),
	script:WaitForChild("Animation2"),
}

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local isChasing = false
local pathFound = false

local function randomWaypoint()
	local waypoint = DestinationsFolder:GetChildren()
	local random = waypoint[math.random(1, #waypoint)]
	return random
end

local function findTarget()
	local nearesttarget
	local maxDistance = 500

	for i,v in ipairs(workspace:WaitForChild("Npcs"):GetChildren()) do
		if v:IsA("Model") and v:FindFirstChildWhichIsA("Humanoid") then
			local human = v:FindFirstChildWhichIsA("Humanoid")

			if v:FindFirstChild("Wanted") and v:FindFirstChild("Wanted").Value == true then
				local distance = (character.PrimaryPart.Position - v.PrimaryPart.Position).Magnitude

				if distance <= maxDistance then
					nearesttarget = v
					maxDistance = distance
				end

				if distance <= 5 then
					humanoid.WalkSpeed = 0

					if human.Health > 0 and os.clock() > nextActivate then
						local chosenAnim = animationTable[combo]

						if chosenAnim then
							local animationTrack = humanoid.Animator:LoadAnimation(chosenAnim)
							combo += 1
							nextActivate = os.clock() + debounceTime
							animationTrack:Play()
							animationTrack.Stopped:Wait()
						end

						if combo > #animationTable then
							combo = 1
						end

						human:TakeDamage(10)
					end
				end
			end
		end
	end
	return nearesttarget
end

local function WalkTo(destination)
	local pathParams = {
		AgentHeight = character:GetExtentsSize().Y,
		AgentRadius = character:GetExtentsSize().Z,
		AgentCanJump = false,
		Costs = {
			Danger = math.huge,
		}
	}

	local path = PathfindingService:CreatePath(pathParams)
	local Blocked = false

	local success, errorMessage = pcall(function()
		path:ComputeAsync(character.PrimaryPart.Position, destination.Position)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()

		path.Blocked:Connect(function(blockedWaypointIndex)
			Blocked = true
			path:ComputeAsync(character.PrimaryPart.Position, destination.Position)
		end)

		for i,v in pairs(waypoints) do
			repeat
				humanoid:MoveTo(v.Position)
			until humanoid.MoveToFinished:Wait() or Blocked

			if Blocked then
				repeat task.wait(1)
					WalkTo(destination)
				until success and path.Status == Enum.PathStatus.Success
			end
		end

		pathFound = false
	end
end

humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	coroutine.wrap(function()
		humanoid.WalkSpeed = 16
		task.wait(10)
		humanoid.WalkSpeed = WalkSpeed
	end)()
end)

local function check()
	for i,v in ipairs(workspace:WaitForChild("Npcs"):GetChildren()) do
		if v:IsA("Model") and v:FindFirstChildWhichIsA("Humanoid") then
			local human = v:FindFirstChildWhichIsA("Humanoid")

			if v:FindFirstChild("Wanted") and v:FindFirstChild("Wanted").Value == false then
				isChasing = false
				humanoid.WalkSpeed = WalkSpeed
				WalkTo(randomWaypoint())
			end
		end
	end
end

RunService.Heartbeat:Connect(function()
	if findTarget() and findTarget() ~= nil then
		isChasing = true
		humanoid.WalkSpeed = 16
		WalkTo(findTarget().PrimaryPart)

	elseif findTarget() == nil then
		if isChasing then
			isChasing = false
			humanoid.WalkSpeed = WalkSpeed
			WalkTo(randomWaypoint())
			
		elseif not isChasing and not pathFound then
			pathFound = true
			humanoid.WalkSpeed = WalkSpeed
			WalkTo(randomWaypoint())
		end
	end
end)
1 Like