I can't help but think that the first part of this code is where things are going wrong (I think $resourcePW is coming up empty when it shouldn't):
/* Do nothing if page is not password-protected, or the form page is not set in the properties */
if ((empty($resourcePW)) || (empty($formResourceID))) {
return;
}
Here's something else to try:
Change this:
$tvPassword = $modx->getOption('tvPassword',$scriptProperties,'pagePassword');
$tvPasswordGroup = $modx->getOption('tvPasswordGroup',$scriptProperties,'pagePasswordGroup');
to this:
$tvPassword = $modx->getOption('tvPassword',$scriptProperties,'pagePassword', true);
$tvPasswordGroup = $modx->getOption('tvPasswordGroup',$scriptProperties,'pagePasswordGroup', true);