Code intermittently works swapping pages with logon

Hello, using 7.9

Long story short, each window requires the user to scan badge to log on. We essentially grab the data from the badge, cross reference a imported file on a table, and use data to logon and swap page provided credentials are accurate.

Which works find most the time. But occasionally the user will scan the badge, we will see the loading icon as if it is going to swap pages. but it stays on the same page. and you have to try logging into a diff page then back into the one you want.

Any idea why it might occasionally hold up and not swap pages but works 90% of the time. My only current idea is the badge reader not reading, but it does show the load icon so it must be working.

below is the code. its rough but “works” i’'m cleaning and revamping it.

# This script was generated automatically by the navigation
# script builder. You may modify this script, but if you do,
# you will not be able to use the navigation builder to update
# this script without overwriting your changes.

password = system.gui.showTouchscreenKeyboard("Scan Badge")

system.nav.openWindow("Main Windows/TableImport")
window = system.gui.getWindow("Main Windows/TableImport")
data = window.rootContainer.getComponent("User Table").data
system.nav.closeWindow("Main Windows/TableImport")
user_name = ""

for row in range(data.rowCount):
	index_row = data.getValueAt(row, 3)
	if index_row == password:
		user_name = data.getValueAt(row, 1)
		break

#Disables Read-Only-Mode, starts timer
success = system.security.switchUser(user_name, password)
roles = system.security.getRoles()
if u'Administrator' in roles or u'LVL_2' in roles or u'LVL_4' in roles:
	system.nav.swapTo('Line 5 Screen')
	system.tag.writeToTag("System1/Project1/TimeOut", 180)
else:
	system.nav.swapTo('Access Denied')
    
system.util.setConnectionMode(3)
system.tag.writeToTag("System1/Project1/ReadModeOnly", 0)
system.tag.writeToTag("System1/Project1/TimerState", 1)


# If the login didn't work, give input focus back to the
# username component, so that the user can try again
if not success:
	system.nav.swapTo('Failed Login')
	system.tag.writeToTag("System1/Project1/ReadModeOnly", 1)
	system.tag.writeToTag("System1/Project1/TimerState", 0)
	system.nav.openWindow("Nav")
	window = system.gui.getWindow("Nav")
	window.rootContainer.getComponent("Timer").value = value