Pathfinding breaks when using pcall

Hi. I recently made a game called CaseOh’s Basics. The pathfinding was working fine, but sometimes I got this error “Pathfinding failed due to a navmesh error” and then the character broke. So I decided to use pcall like so:

local Success, Error = pcall(function()
-- move to somewhere or pathfinding
end)

But whenever I tried using that it sometimes made my character lag. Like he went forward, then a bit backwards and again forward.
Please help. Thanks.

I would appreciate if someone told me how to fix the “navmesh error” and “pathfinding lagging when using pcall”. Full script of one of the characters with the issue:

local SearchDistance=15000
local hroot=script.Parent:WaitForChild("HumanoidRootPart")
local human=script.Parent:WaitForChild("Nextbot")

local path
local waypoint
local oldpoints
local isWandering = 0

hroot.Walk:Play()

local function getNearestTorso()
	-- get nearest the nearest player, nearest distance, nearest character to our humanoid root part
	local nearesttorso = nil
	local nearestdistance = SearchDistance
	local neareastchar = nil
	local characters = game.Workspace:GetChildren()
	for _, v in pairs(characters) do
		if v:IsA'Model' and v ~= script.Parent and v:FindFirstChild("Player_Val") then
			local charroot = v:FindFirstChild'HumanoidRootPart'
			if charroot then
				local distance = (charroot.Position - hroot.Position).magnitude
				if distance < nearestdistance then
					nearestdistance = distance
					neareastchar = v
					nearesttorso = charroot
					
					--print(nearesttorso)
				end
			end
		end
	end
	
	return nearesttorso

end



while wait() do
	
	local enemytorso = getNearestTorso()	
	
	if enemytorso ~= nil and (enemytorso.Position - hroot.Position).magnitude <= SearchDistance then -- if player detected
		-- pathfind to the torso
		local path = game:GetService("PathfindingService"):ComputeRawPathAsync(hroot.Position, enemytorso.Position, SearchDistance)
		if path.Status ~= Enum.PathStatus.FailFinishNotEmpty then -- if path is computed
			local points = path:GetPointCoordinates()
			if #points < 500 then -- if path is short enough
				-- start pathfinding
				isWandering = 1
				oldpoints = points
				waypoint = 1
				human.MoveToFinished:connect(function()
					if waypoint < #oldpoints then
						waypoint = waypoint + 1
						local Success, Error = pcall(function()
						human.WalkToPoint = oldpoints[waypoint]
						end)
					else
						isWandering = 0
					end
				end)
				waypoint = waypoint + 1
				if oldpoints[waypoint] then
					local Success, Error = pcall(function()
					human.WalkToPoint = oldpoints[waypoint]
					end)
				end
			end
		end
	end
	
end

You can try making your script non-pcall. From what I remember, pathfinding service doesn’t stop the script if it errors (note: this may be incorrect, it is from memory), alternatively you can check the PathStatus, if it is not correct, return.

can you please try this and let me know your results, thanks :smile:

1 Like

It seems to be working! Thank you. I will mark this as the solution in a few hours or tommorow, because I’m still not 100 % sure if it will work all the time and not break from navmesh error or something like that.

Thank you!
clapper

3 Likes

I have 1 issue. The characters still do walk forward, then back a bit and forward again. Can you test this script in your game to maybe replicate the bug and help me fix it? Ignore the Speed Modifier bit. Its always set to 1.

local SearchDistance=15000
local hroot=script.Parent:WaitForChild("HumanoidRootPart")
local human=script.Parent:WaitForChild("Nextbot")

local path
local waypoint
local oldpoints
local isWandering = 0

hroot.Walk:Play()

local function getNearestTorso()
	-- get nearest the nearest player, nearest distance, nearest character to our humanoid root part
	local nearesttorso = nil
	local nearestdistance = SearchDistance
	local neareastchar = nil
	local characters = game.Workspace:GetChildren()
	for _, v in pairs(characters) do
		if v:IsA'Model' and v ~= script.Parent and v:FindFirstChild("Player_Val") then
			local charroot = v:FindFirstChild'HumanoidRootPart'
			if charroot then
				local distance = (charroot.Position - hroot.Position).magnitude
				if distance < nearestdistance then
					nearestdistance = distance
					neareastchar = v
					nearesttorso = charroot
					
					--print(nearesttorso)
				end
			end
		end
	end
	
	return nearesttorso

end

workspace.CONFIG.SpeedModifier:GetPropertyChangedSignal("Value"):Connect(function()
	human.WalkSpeed=human.WalkSpeed*workspace.CONFIG.SpeedModifier.Value
end)

while wait() do
	
	local enemytorso = getNearestTorso()	
	
	if enemytorso ~= nil and (enemytorso.Position - hroot.Position).magnitude <= SearchDistance then -- if player detected
		-- pathfind to the torso
		local path = game:GetService("PathfindingService"):ComputeRawPathAsync(hroot.Position, enemytorso.Position, SearchDistance)
		if path.Status ~= Enum.PathStatus.FailFinishNotEmpty then -- if path is computed
			local points = path:GetPointCoordinates()
			if #points < 500 and path.Status==Enum.PathStatus.Success then -- if path is short enough
				-- start pathfinding
				print("correct")
				isWandering = 1
				oldpoints = points
				waypoint = 1
				human.MoveToFinished:connect(function()
					if waypoint < #oldpoints then
						waypoint = waypoint + 1
						--local Success, Error = pcall(function()
						human.WalkToPoint = oldpoints[waypoint]
						--end)
					else
						isWandering = 0
					end
				end)
				waypoint = waypoint + 1
				if oldpoints[waypoint] and path.Status==Enum.PathStatus.Success then
					--local Success, Error = pcall(function()
					human.WalkToPoint = oldpoints[waypoint]
					--end)
				end
			end
		end
	end
	
end

Thanks for letting me know of the issue. I’ll test the script in my place, and I will do some thorough research

I’ll try to help as much as I can :smile: I’ll get back to you when I have found something

1 Like

I wasn’t able to replicate the issue you were talking about. For me, the character seems to always walk towards me, which I think is the correct behavior? I didn’t notice any visible “setbacks”

Do you maybe have some kind of lag, or latency?
Video result:

1 Like

Okay, I think it’s just lag. But thanks ! :sunglasses::facepunch:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.