Ai humanoid stop completely even if path is clear

I made bumpercar humanoid, and move it with path finding and MoveTo, the point to move to is random generated, but at some point it completley stop to move, even if the path is clear.

As you can see by the path marker the path is clear, but the car does not move

I tried with spawn and not spawn, i tries to print out diffrent vairbales, the path is Enum.PathStatus.Success

it sday true,
true – 8 – 11
true – 9 – 11

even when it does not move

I tried adde agent raidius does not seem to help

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!


local PathfindingService = game:GetService("PathfindingService")


local pathParems = {
	AgentRadius = 7, 
	AgentHeight = 15, 
	AgentCanJump = false
}

local path = PathfindingService:CreatePath(pathParems)


-- Variables to store waypoints table and zombie's current waypoint
local waypoints
local currentWaypointIndex
local destination
local humanoidz

local function followPath(destinationVector,zombiez)
	-- Compute and check the path
	path = PathfindingService:FindPathAsync(zombiez.HumanoidRootPart.Position,destinationVector)
	--path:ComputeAsync(zombiez.HumanoidRootPart.Position, destinationVector)
	-- Empty waypoints table after each new path computation
	waypoints = {}
	
	print(path.Status)

	if path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints and start zombie walking
		waypoints = path:GetWaypoints()
		
		for i, v in pairs(waypoints) do
			local new = Instance.new("Part")
			new.Position = v.Position
			new.Anchored = true
			new.CanCollide = false
			new.Parent = game.Workspace
			
			game:GetService("Debris"):AddItem(new, 2)
		end

		-- Move to first waypoint
		currentWaypointIndex = 1
		
		print(currentWaypointIndex.." / "..#waypoints)
		humanoidz:MoveTo(waypoints[currentWaypointIndex].Position)
	else
		-- Error (path not found); stop humanoid
		humanoidz:MoveTo(zombiez.HumanoidRootPart.Position)
	end
end

local function onWaypointReached(reached)
	print(tostring(reached).." -- " .. currentWaypointIndex .. " -- " .. #waypoints)
	if reached and currentWaypointIndex < #waypoints then
		currentWaypointIndex = currentWaypointIndex + 1
		humanoidz:MoveTo(waypoints[currentWaypointIndex].Position)
	end
end

--variable to definae ara


local x1 = script.Parent.Parent.x1.Value
local x2 = script.Parent.Parent.x2.Value


local z1 = script.Parent.Parent.z1.Value
local z2 = script.Parent.Parent.z2.Value



local function onPathBlocked(blockedWaypointIndex)
	print("##Path blocket##")
	print(blockedWaypointIndex)
	print(currentWaypointIndex)
	-- Check if the obstacle is further down the path
	if blockedWaypointIndex > currentWaypointIndex then
		-- Call function to re-compute the path
		
		local xp = math.random(x1,x2)
		local zp = math.random(z1,z2)

		local mp = Vector3.new(xp,38.158,zp)
		print(destination)
		destination = mp
		print(destination)
		
		followPath(destination)
	end
end

-- Connect 'Blocked' event to the 'onPathBlocked' function
path.Blocked:Connect(onPathBlocked)





--function

local function MoveCar(car)
	--spawn(function()
	local xp = math.random(x1,x2)
	local zp = math.random(z1,z2)

	local mp = Vector3.new(xp,38.158,zp)

	local destination = mp
	humanoidz = car.Humanoid
	
	path = PathfindingService:CreatePath(pathParems)
	
	humanoidz.MoveToFinished:Connect(onWaypointReached)
	path.Blocked:Connect(onPathBlocked)
	currentWaypointIndex = nil
	followPath(destination,car)
	--humanoidz:MoveTo(mp)
	--end)
end


--MoveCar(script.Parent.BumperCars.BpCar)

local BumperCars = script.Parent

while true do

	--spawn(function()
		MoveCar(BumperCars)	
	--end)

	wait(4)
	path:Destroy()
	waypoints = nil
end

I’ve experienced this before with the Wiki pathfinding code. Basically the humanoid gets stuck in one of the MoveTo functions, it’s trying to walk to that position forever, usually it gets like .2 studs away and just never fires the onWaypointReached event so it’s indefinitely stuck waiting.
You’ll need to add your own logic to handle if your humanoid gets stuck and what to do when it does.

A simple way to do this…

    local lastPosition = BumperCars.HumanoidRootPart.Position
    local stuckCount = 0
    local isPathfinding = false
    while true do
            local newPosition = BumperCars.HumanoidRootPart.Position
            if not isPathfinding then
                isPathfinding = true
    	    spawn(function()
    		 MoveCar(BumperCars)	
                     isPathfinding = false
    	    end)
            else
                 if (newPosition - lastPosition).magnitude < .3 then
                      stuckCount += 1
                 else
                      stuckCount = 0
                 end
                 if stuckCount >= 5 then
                        isPathfinding = false --this will cause a new MoveCar to be spawned, overriding the old path
                 end
            end
            lastPosition = newPosition
    	wait(1)
    	--path:Destroy()  you dont have to destroy this, when followpath is called again and it's overwritten, it'll get cleaned up
    	-- waypoints = nil --same with this one being set to {}
    end

Thank you, but seems to stop completely still.

I have tried implented another function to handle it, but it still seems to do nothing and get stuck

Output:

  16:57:27.822  newcar  -  Server - MoveBPCarsGul:137
  16:57:27.822  -7369.1416, 37.5668488, -465.626556  -  Server - MoveBPCarsGul:138
  16:57:27.822  -7369.1416, 37.5677757, -465.626556  -  Server - MoveBPCarsGul:139
  16:57:27.822  newcar  -  Server - MoveBPCarsGul:140
  16:57:27.822  @@@0.00092697143554688@@@  -  Server - MoveBPCarsGul:143
  16:57:27.823  here  -  Server - MoveBPCarsGul:146
  16:57:27.823  -7369.1416015625  -  Server - MoveBPCarsGul:147
  16:57:27.823  -7369.1416015625  -  Server - MoveBPCarsGul:148
  16:57:27.823  here  -  Server - MoveBPCarsGul:149
  16:57:27.823  -465.62655639648  -  Server - MoveBPCarsGul:150
  16:57:27.823  -465.62655639648  -  Server - MoveBPCarsGul:151
  16:57:27.823  here  -  Server - MoveBPCarsGul:152
  16:57:27.824  new follow  -  Server - MoveBPCarsGul:154
  16:57:27.824  -7374.1416, 37.5668488, -470.626556  -  Server - MoveBPCarsGul:161
  16:57:35.888  stopfollowing  -  Server - MoveBPCarsGul:164

local PathfindingService = game:GetService("PathfindingService")


local pathParems = {
	AgentRadius = 7, 
	AgentHeight = 15, 
	AgentCanJump = false
}

local path = PathfindingService:CreatePath(pathParems)


-- Variables to store waypoints table and zombie's current waypoint
local waypoints
local currentWaypointIndex
local destination
local humanoidz

local function followPath(destinationVector,zombiez)
	-- Compute and check the path
	path = PathfindingService:FindPathAsync(zombiez.HumanoidRootPart.Position,destinationVector)
	--path:ComputeAsync(zombiez.HumanoidRootPart.Position, destinationVector)
	-- Empty waypoints table after each new path computation
	waypoints = {}

	--print(path.Status)

	if path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints and start zombie walking
		waypoints = path:GetWaypoints()

		for i, v in pairs(waypoints) do
			local new = Instance.new("Part")
			new.Position = v.Position
			new.Anchored = true
			new.CanCollide = false
			new.Parent = game.Workspace

			game:GetService("Debris"):AddItem(new, 1)
		end

		-- Move to first waypoint
		currentWaypointIndex = 1

		--print(currentWaypointIndex.." / "..#waypoints)
		humanoidz:MoveTo(waypoints[currentWaypointIndex].Position)
	else
		-- Error (path not found); stop humanoid
		humanoidz:MoveTo(zombiez.HumanoidRootPart.Position)
	end
end

local function onWaypointReached(reached)
	--print(tostring(reached).." -- " .. currentWaypointIndex .. " -- " .. #waypoints)
	if reached and currentWaypointIndex < #waypoints then
		currentWaypointIndex = currentWaypointIndex + 1
		humanoidz:MoveTo(waypoints[currentWaypointIndex].Position)
	end
end

--variable to definae ara


local x1 = script.Parent.Parent.x1.Value
local x2 = script.Parent.Parent.x2.Value


local z1 = script.Parent.Parent.z1.Value
local z2 = script.Parent.Parent.z2.Value



local function onPathBlocked(blockedWaypointIndex)
	--print("##Path blocket##")
	--print(blockedWaypointIndex)
	--print(currentWaypointIndex)
	-- Check if the obstacle is further down the path
	if blockedWaypointIndex > currentWaypointIndex then
		-- Call function to re-compute the path

		local xp = math.random(x1,x2)
		local zp = math.random(z1,z2)

		local mp = Vector3.new(xp,38.158,zp)
		--print(destination)
		destination = mp
		--print(destination)

		followPath(destination)
	end
end

-- Connect 'Blocked' event to the 'onPathBlocked' function
path.Blocked:Connect(onPathBlocked)





--function

local function MoveCar(car)
	--spawn(function()
	local xp = math.random(x1,x2)
	local zp = math.random(z1,z2)

	local mp = Vector3.new(xp,38.158,zp)

	local destination = mp
	humanoidz = car.Humanoid

	path = PathfindingService:CreatePath(pathParems)

	humanoidz.MoveToFinished:Connect(onWaypointReached)
	path.Blocked:Connect(onPathBlocked)
	currentWaypointIndex = 0
	followPath(destination,car)
	--humanoidz:MoveTo(mp)
	--end)
end


--MoveCar(script.Parent.BumperCars.BpCar)

local BumperCars = script.Parent

local lastPosition = BumperCars.HumanoidRootPart.Position
local stuckCount = 0
local isPathfinding = false
local init = true


while true do
	local newPosition = BumperCars.HumanoidRootPart.Position
	if not isPathfinding then
		print("newcar")
		print(newPosition)
		print(lastPosition)
		print("newcar")
		isPathfinding = true

		print("@@@"..(newPosition - lastPosition).magnitude.."@@@")

		if (newPosition - lastPosition).magnitude < .3 and init == false then
			print("here")
			print(newPosition.X)
			print(lastPosition.X)
			print("here")
			print(newPosition.Z)
			print(lastPosition.Z)
			print("here")
			if newPosition.X == lastPosition.X and newPosition.Z == lastPosition.Z then
				print("new follow")
				stuckCount = stuckCount + 1
				if stuckCount >= 5 then
					isPathfinding = true
					--BumperCars:MoveTo(Vector3.new(newPosition.X-10,newPosition.Y+0.00000001,newPosition.Z-10))
					currentWaypointIndex = 0
					local vec = Vector3.new(newPosition.X-5,newPosition.Y+0.00000001,newPosition.Z-5)
					print(vec)
					followPath(vec,BumperCars)
					wait(8)
					print("stopfollowing")
					isPathfinding = false
					stuckCount = 0
				end
			end
		end

		spawn(function()
			MoveCar(BumperCars)	
			isPathfinding = false
			init = false
		end)
	else
		print(newPosition)
		print(lastPosition)		
		print("###"..(newPosition - lastPosition).magnitude.."###")
		print(stuckCount)
		if (newPosition - lastPosition).magnitude < .3 then
			stuckCount += 1
		else
			stuckCount = 0
		end
		if stuckCount >= 5 then
			isPathfinding = false --this will cause a new MoveCar to be spawned, overriding the old path
		end
	end
	lastPosition = newPosition
	wait(1)
	--path:Destroy()  you dont have to destroy this, when followpath is called again and it's overwritten, it'll get cleaned up
	-- waypoints = nil --same with this one being set to {}
end

The error was a kill script killing the humanoid, by changing that to exclude the cars, it seems never to stop, but implemented stop handler anyways.

--langt script
local MainPart = script.Parent

function deadthAllThePartsIn(parent)
	for _, child in ipairs(parent:GetChildren()) do
		if child:IsA("BasePart") then
			child.Touched:Connect(function(hit)
				if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
					if hit.Parent.Name ~= "BpCar" then
						hit.Parent.Humanoid.Health = 0
					end
				end	
			end)
		elseif child:IsA("Model") then
			deadthAllThePartsIn(child) -- recursive step
		end
	end
end

deadthAllThePartsIn(MainPart)

Move car script:

local PathfindingService = game:GetService("PathfindingService")


local pathParems = {
	AgentRadius = 7, 
	AgentHeight = 15, 
	AgentCanJump = false
}

local path = PathfindingService:CreatePath(pathParems)


-- Variables to store waypoints table and zombie's current waypoint
local waypoints
local currentWaypointIndex
local destination
local humanoidz
local stopthread = false

local function followPath(destinationVector,zombiez)
	-- Compute and check the path
	path = PathfindingService:FindPathAsync(zombiez.HumanoidRootPart.Position,destinationVector)
	--path:ComputeAsync(zombiez.HumanoidRootPart.Position, destinationVector)
	-- Empty waypoints table after each new path computation
	waypoints = {}

	if path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints and start zombie walking
		waypoints = path:GetWaypoints()

		-- Move to first waypoint
		currentWaypointIndex = 1

		humanoidz:MoveTo(waypoints[currentWaypointIndex].Position)
	else
		-- Error (path not found); stop humanoid
		humanoidz:MoveTo(zombiez.HumanoidRootPart.Position)
	end
end

local function onWaypointReached(reached)
	if reached and currentWaypointIndex < #waypoints then
		if stopthread == false then
			currentWaypointIndex = currentWaypointIndex + 1
			humanoidz:MoveTo(waypoints[currentWaypointIndex].Position)
		end
	end
end

--variable to definae ara


local x1 = script.Parent.Parent.x1.Value
local x2 = script.Parent.Parent.x2.Value


local z1 = script.Parent.Parent.z1.Value
local z2 = script.Parent.Parent.z2.Value



local function onPathBlocked(blockedWaypointIndex)
	-- Check if the obstacle is further down the path
	if blockedWaypointIndex > currentWaypointIndex then
		-- Call function to re-compute the path

		local xp = math.random(x1,x2)
		local zp = math.random(z1,z2)

		local mp = Vector3.new(xp,38.158,zp)

		destination = mp

		followPath(destination)
	end
end

-- Connect 'Blocked' event to the 'onPathBlocked' function
path.Blocked:Connect(onPathBlocked)





--function

local function MoveCar(car)
	--spawn(function()
	local xp = math.random(x1,x2)
	local zp = math.random(z1,z2)

	local mp = Vector3.new(xp,38.158,zp)

	local destination = mp
	humanoidz = car.Humanoid

	path = PathfindingService:CreatePath(pathParems)

	humanoidz.MoveToFinished:Connect(onWaypointReached)
	path.Blocked:Connect(onPathBlocked)
	currentWaypointIndex = 0
	followPath(destination,car)
end

local BumperCars = script.Parent

local lastPosition = BumperCars.HumanoidRootPart.Position

local orgposition = BumperCars.HumanoidRootPart.Position
local stuckCount = 0
local isPathfinding = false
local init = true



while true do
	local newPosition = BumperCars.HumanoidRootPart.Position
	if not isPathfinding then

		isPathfinding = true

		if (newPosition - lastPosition).magnitude < .3 and init == false then

			if newPosition.X == lastPosition.X and newPosition.Z == lastPosition.Z then

				stuckCount = stuckCount + 1
				if stuckCount >= 5 then
					isPathfinding = true

					currentWaypointIndex = 0

					local vec = orgposition
					destination = vec

					BumperCars.PrimaryPart.CFrame = BumperCars.PrimaryPart.CFrame * CFrame.new(0,0.1,0)
					
					path = PathfindingService:FindPathAsync(BumperCars.HumanoidRootPart.Position,vec)

					waypoints = {}
					
					waypoints = path:GetWaypoints()
					
					stopthread = true

					for i, v in pairs(waypoints) do
						
						BumperCars.Humanoid:MoveTo(v.Position)
						--BumperCars.Humanoid.MoveToFinished:Wait()
						wait(0.5)
					end
					
					
					
					wait(8)
					stopthread = false
					isPathfinding = false
					stuckCount = 0
				end
			end
		end

		spawn(function()
			MoveCar(BumperCars)	
			isPathfinding = false
			init = false
		end)
	else
		if (newPosition - lastPosition).magnitude < .3 then
			stuckCount += 1
		else
			stuckCount = 0
		end
		if stuckCount >= 5 then
			isPathfinding = false --this will cause a new MoveCar to be spawned, overriding the old path
		end
	end
	lastPosition = newPosition
	wait(1)
end ```