Attempt to index field a 'parent' (a nil value) error

I am creating a game in which a boat sails in a river, collect the coins and when it collides with rock it needs to stop and user need to restart the game.I can able to do everything in story board but unable to restart the game .

 

[lua]

local coin = {}

local coinCounter = 1

local tmr_createCoin

local coinCreationSpeed = 500

local coinMovementSpeed = 3000

local Stone = {}

local StoneCounter = 1

local tmr_createStone

local StoneCreationSpeed = 3000

local StoneMovementSpeed = 4000

function createCoins()

local startingPosition = math.random(1,3)

if(startingPosition == 1) then

startingX = math.random(520 , 560)

startingY = 210

elseif(startingPosition == 2) then

startingX = math.random(520 , 560)

startingY = 250

else

startingX = math.random(520 , 560)

startingY = 290

end

coin[coinCounter] = display.newImage(“gold.png”);

coin[coinCounter].x = startingX

coin[coinCounter].y = startingY

physics.addBody( coin[coinCounter], “static”, { isSensor=true} )

coin[coinCounter].type = “coin”

myGroup:insert(coin[coinCounter])

coin[coinCounter].trans = transition.to(coin[coinCounter], {

time=coinMovementSpeed, x = -20 ,

onComplete = function (self)

self.parent:remove(self);

self = nil;

end;

})

coinCounter = coinCounter + 1

end

function createStones()

local startingPosition = math.random(1,3)

if(startingPosition == 1) then

startingX = 500

startingY = 210

elseif(startingPosition == 2) then

startingX = 500

startingY = 250

else

startingX = 500

startingY = 290

end

Stone[StoneCounter] = display.newImage(“rocks.png”);

Stone[StoneCounter].x = startingX

Stone[StoneCounter].y = startingY

physics.addBody( Stone[StoneCounter], physicsData:get(“rocks”) )

Stone[StoneCounter].type = “rocks”

myGroup:insert(Stone[StoneCounter])

Stone[StoneCounter].trans = transition.to(Stone[StoneCounter], {

time=StoneMovementSpeed, x = -20 ,

onComplete = function (self)

self.parent:remove(self);

self = nil;

end;

})

StoneCounter = StoneCounter + 1

end 

local function onLocalCollision( event )

if(event.object1.type == “player” and event.object2.type == “rocks” ) then

print(“stone”)

transition.cancel(event.object2.trans)

timer.cancel(tmr_createCoin)

timer.cancel(tmr_createStone)

Runtime:removeEventListener(“enterFrame” , backbg)

Runtime:removeEventListener(“enterFrame” , backbg1)

Runtime:removeEventListener(“enterFrame” , frontbg)

Runtime:removeEventListener(“enterFrame” , frontbg1)

local txt_gameover = display.newText(“Game Over!”,0,0,native.systemFont,32)

txt_gameover.x = 100

txt_gameover.y = 200

myGroup:insert(txt_gameover)

local function onGameOverTouch(event)

if(event.phase == “began”) then

storyboard.gotoScene(“menu”)

end

end

local txt_gameover = display.newText(“Return To Menu”,0,0,native.systemFont,32)

txt_gameover.x = 200

txt_gameover.y = 300

txt_gameover:addEventListener(“touch”,onGameOverTouch)

myGroup:insert(txt_gameover)

end

if(event.object1.type == “player” and event.object2.type == “coin” ) then

print(“coin”)

event.object2.alpha = 0

transition.cancel(event.object2.trans)

event.object2:removeSelf()

event.object2 = nil

elseif( event.object1.type == “coin” and event.object2.type == “player”) then

print(“coin”)

event.object1.alpha = 0

transition.cancel(event.object1.trans)

event.object1:removeSelf()

event.object1 = nil

end

end

Runtime:addEventListener( “collision”, onLocalCollision )

[/lua]

Can you post the whole text of the error from your console log?

Also I’m not sure where your source originated, but your code is not indented in a way that we can read it without a lot of work.  Please post your code in a way that it maintains it’s indents. If you are not indenting your code you really should consider it.  See:

https://coronalabs.com/blog/2015/06/09/tutorial-the-value-of-well-formatted-code/

Rob

Can you post the whole text of the error from your console log?

Also I’m not sure where your source originated, but your code is not indented in a way that we can read it without a lot of work.  Please post your code in a way that it maintains it’s indents. If you are not indenting your code you really should consider it.  See:

https://coronalabs.com/blog/2015/06/09/tutorial-the-value-of-well-formatted-code/

Rob