Writing data into text file in lua with scenes

for my school project I am suppose to save some game data or reset into a text file. I have implemented a reset button to reset all the game values into 0, it does writes the values into the text file but as soon as that method ends game returns to the home screen which is not desirable (Suppose to reset the scores and stay in the same scene). Any idea why this is happening ?(I have located the origin of the error in )

score board scene:

--Student Name: Charith Vikum Lakmal Jayawardana --Student ID: 10449312 -- ----------------------------------------------------------------------------------- -- Importing Libraries -- ----------------------------------------------------------------------------------- local composer = require( "composer" ) local widget = require("widget") local scene = composer.newScene() local path = system.pathForFile("scores.txt", system.ResourceDirectory) ------------------------------------------------------- -- Display and Game Parameters ------------------------------------------------------- -- Propotions of the display as % d = display w20 = d.contentWidth \* .2 h20 = d.contentHeight \* .2 w40 = d.contentWidth \* .4 h40 = d.contentHeight \* .4 w60 = d.contentWidth \* .6 h60 = d.contentHeight \* .6 w80 = d.contentWidth \* .8 h80 = d.contentHeight \* .8 local winCounter local lostCounter local drawCounter -- Assign center of display X n Y into variables local centX = display.contentCenterX local centY = display.contentCenterY --String split function given in corona libraries function string:split( inSplitPattern ) local outResults = {} local theStart = 1 local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) while theSplitStart do table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) ) theStart = theSplitEnd + 1 theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart ) end table.insert( outResults, string.sub( self, theStart ) ) return outResults end --get the game data into counters local function getGameData( path ) local contents local gameDataTable --open the text file in read mode local file, errorString = io.open( path, "r" ) if not file then print( "File error: " .. errorString ) else -- Read data from file contents = file:read( "\*l" ) io.close( file ) gameDataTable = contents:split( ", " ) end winCounter = gameDataTable[1] lostCounter = gameDataTable[2] drawCounter = gameDataTable[3] file = nil end local function handleResetButtonEvent( event ) if ("ended"== event.phase) then winCounter = 0 lostCounter = 0 drawCounter = 0 local saveData = winCounter .. ", " ..lostCounter..", "..drawCounter --open the text file in write mode local file,error = io.open(path,"w") if not file then print(error) else file:write(saveData) io.close(file) end file = nil end end local function handleGoMainButtonEvent( event ) if ("ended"== event.phase) then composer.gotoScene( "homeScreen", {time = 600, effect = "fromTop"} ) end end -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) local sceneGroup = self.view -- Background image local background = display.newImageRect( sceneGroup, "background.jpg", 800, 1400) background.x = centX background.y = centY -- Title image local title = display.newImageRect( sceneGroup, "images/scoreScreenTitle.png", 302, 75) title.x = centX title.y = 60 local win = display.newImageRect( sceneGroup, "images/wins.png", 101, 55) win.x = centX-50 win.y = 150 local loss = display.newImageRect( sceneGroup, "images/losses.png", 120, 55) loss.x = centX-50 loss.y = 200 local draw = display.newImageRect( sceneGroup, "images/draws.png", 120, 55) draw.x = centX-50 draw.y = 250 -- read game data from the text file getGameData(path) winText = display.newText(sceneGroup,winCounter,w40+30,h80+30,"Verdana",30) winText.x = centX +50 winText.y = 155 lostText = display.newText(sceneGroup,lostCounter,w40+30,h80+30,"Verdana",30) lostText.x = centX +50 lostText.y = 200 drawText = display.newText(sceneGroup,drawCounter,w40+30,h80+30,"Verdana",30) drawText.x = centX +50 drawText.y = 250 -- Rest button local resetButton = widget.newButton( { left = centX-60, top = h80-20, id = "reset",label = "Reset",onEvent = handleResetButtonEvent, --Properties shape = "roundedRect", width = 110, height = 40, font = "Bauhaus 93", emboss = true, cornerRadius = 2, fontSize = 40, fillColor = {default = {138, 43, 226}, over = { 0, 0, 0, 0.5 }}, strokeColor = {default={1,0,4,0,1}, over = {0.8,0.8,1,1}}, stroeWidth = 4 } ) sceneGroup:insert(resetButton) -- inster button into Scene Group to limit the scope of UI into this scene only -- goMain button local goMain = widget.newButton( { left = centX-95, top = h80+40, id = "goMain",label = "Main Menu",onEvent = handleGoMainButtonEvent, --Properties shape = "roundedRect", width = 180, height = 40, font = "Bauhaus 93", emboss = true, cornerRadius = 2, fontSize = 35, fillColor = {default = {138, 43, 226}, over = { 0, 0, 0, 0.5 }}, strokeColor = {default={1,0,4,0,1}, over = {0.8,0.8,1,1}}, stroeWidth = 4 } ) sceneGroup:insert(goMain) -- inster button into Scene Group to limit the scope of UI into this scene only end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene

main menu scene: 

--Student Name: Charith Vikum Lakmal Jayawardana --Student ID: 10449312 -- ----------------------------------------------------------------------------------- -- Importing Libraries -- ----------------------------------------------------------------------------------- local composer = require( "composer" ) local widget = require("widget") local scene = composer.newScene() ------------------------------------------------------- -- Display and Game Parameters ------------------------------------------------------- -- Propotions of the display as % d = display w20 = d.contentWidth \* .2 h20 = d.contentHeight \* .2 w40 = d.contentWidth \* .4 h40 = d.contentHeight \* .4 w60 = d.contentWidth \* .6 h60 = d.contentHeight \* .6 w80 = d.contentWidth \* .8 h80 = d.contentHeight \* .8 -- Assign center of display X n Y into variables local centX = display.contentCenterX local centY = display.contentCenterY --Event handller for Start button local function handleStartButtonEvent( event ) if ("ended"== event.phase) then composer.gotoScene( "gameScreen", {time = 600, effect = "fromBottom"} ) -- game flow will directed to Game Screen end end local function handleScoreButtonEvent( event ) if ("ended"== event.phase) then composer.gotoScene( "scoreScreen", {time = 600, effect = "fromTop"} ) end end --Event handller for Exit button local function handleExitButtonEvent( event ) if ("ended"== event.phase) then os.exit() end end -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) local sceneGroup = self.view -- Background image local background = display.newImageRect( sceneGroup, "background.jpg", 800, 1400) background.x = centX background.y = centY -- Title image local title = display.newImageRect( sceneGroup, "homeScreenTitle.png", 302, 75) title.x = centX+10 title.y = 60 -- Logo image local logo = display.newImageRect( sceneGroup, "logo.png", 200, 200) logo.x = centX logo.y = centY -- Start button local startButton = widget.newButton( { left = centX-50, top = h80-20, id = "start",label = "Start",onEvent = handleStartButtonEvent, --Properties shape = "roundedRect", width = 100, height = 40, font = "Bauhaus 93", emboss = true, cornerRadius = 2, fontSize = 40, fillColor = {default = {138, 43, 226}, over = { 0, 0, 0, 0.5 }}, strokeColor = {default={1,0,4,0,1}, over = {0.8,0.8,1,1}}, stroeWidth = 4 } ) sceneGroup:insert(startButton) -- inster button into Scene Group to limit the scope of UI into this scene only local scoreButton = widget.newButton( { left = centX-81, top = h80+35, id = "score",label = "Score Board",onEvent = handleScoreButtonEvent, --Properties shape = "roundedRect", width = 165, height = 35, font = "Bauhaus 93", emboss = true, cornerRadius = 2, fontSize = 30, fillColor = {default = {138, 43, 226}, over = { 0, 0, 0, 0.5 }}, strokeColor = {default={1,0,4,0,1}, over = {0.8,0.8,1,1}}, stroeWidth = 4 } ) sceneGroup:insert(scoreButton) -- inster button into Scene Group to limit the scope of UI into this scene only -- Start button local exitButton = widget.newButton( { left = centX-41, top = h80+80, id = "exit",label = "Exit",onEvent = handleExitButtonEvent, --Properties shape = "roundedRect", width = 85, height = 35, font = "Bauhaus 93", emboss = true, cornerRadius = 2, fontSize = 40, fillColor = {default = {138, 43, 226}, over = { 0, 0, 0, 0.5 }}, strokeColor = {default={1,0,4,0,1}, over = {0.8,0.8,1,1}}, stroeWidth = 4 } ) sceneGroup:insert(exitButton) -- inster button into Scene Group to limit the scope of UI into this scene only end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene