How can i fix my pathfinding?

I have this pathfinding script that is wrapped in a pcall function, but sometimes for seemingly no reason, it fails, and the path.Status is no path. Heres my script, sorry that its poorly structured.

function module.Q(plr, char, hitbox, explosion)
	
	task.wait(.2)
	
	local parts = {}
	
	local nuggetMinion = Minion:Clone()
	nuggetMinion.Parent = workspace.InGameChars.Other
	
	nuggetMinion.Name = "Minion"
	
	nuggetMinion:PivotTo(plr.Character:GetPivot())
	
	for i, v in workspace:GetDescendants() do
		if v:GetAttribute("Vulnerable") then
			table.insert(parts, v)
		end
	end
	
	local ranNum = math.random(1, #parts)
	local targetPart = workspace:FindFirstChild(parts[ranNum].Name, true)
	
	hitbox.Parent = workspace
	hitbox.Transparency = 1
	hitbox.Position = nuggetMinion.HumanoidRootPart.Position
	hitbox.Anchored = false
	
	local weld = Instance.new("WeldConstraint")
	weld.Parent = nuggetMinion
	weld.Part0 = nuggetMinion.HumanoidRootPart
	weld.Part1 = hitbox
	
	local humanoid = nuggetMinion:WaitForChild("Humanoid")

	local path = pathFindingService:CreatePath({
		AgentCanJump = true,
	})
	
	local success, errorMessage = pcall(function()
		path:ComputeAsync(nuggetMinion.HumanoidRootPart.Position, targetPart:GetPivot().Position)
	end)

	
	if success and path.Status == Enum.PathStatus.Success then
		
		
		for i, waypoint in pairs(path:GetWaypoints()) do
			humanoid:MoveTo(waypoint.Position)
			
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				humanoid.Jump = true 
			end
			
			humanoid.MoveToFinished:Wait()
		end
		
		local tabled = workspace:GetPartBoundsInBox(hitbox.CFrame, hitbox.Size)
		for i, v in tabled do
			local parent = v
			
			while parent do
		
				if parent:GetAttribute("Vulnerable") then
					for i, v in parent:GetDescendants() do
						if v:IsA("BasePart") then
							v.Anchored = false
						end
					end
					
					explosion.Position = parent:GetPivot().Position
					explosion.Parent = parent
					
					destroyedEvent:Fire(parent)
				end
				parent = parent.Parent
			end
		end
		
		task.wait(2)
		
		nuggetMinion:Destroy()
		
	else
		warn("Path was created Unsuccesfullly")
	end
end

It works fine, but sometimes the npc will be spawned but ill get the error saying path was unnsuccesful. This is strange because i can see the target fine, and i know they can make it. Can anyone help?

roblox pathfinding is kind of broken

try adjusting agent height and radius

you could do something like moving randomly and trying a new path until one is found

1 Like