Level unlock glitch

I use the code below to unlock levels in a table which switchs from 1, 2, or 3 for unlocked locked and completed. the problem is it doesnt work half the time when you go back to the level menu and half the time it does. then it also works if you beat the level then refresh the simulator or reload the game. I don’t know if this is the software or my game. I have to later put in a if statement, but for now I’m just trying to understand if the code is the problem or the software.

function levelMod(event) levels[1] = 3 levels[2] = 1 composer.gotoScene(event.target.destination,{effect="fade"}) return true end