Error with id printing for crates

This is not really an error, but it does not work the way I want it to work. What I have done is a gave every crate an id that matches a label I placed on them. The problem is I want it to print if a crate has gone off the screen, I have it on an enterFrame listener, but it only prints it for on crate. Why is this happening? This is my code, also, how could I get it to work for the crates that immediately spawn. Right now, I am only making it for crates the user spawns:

for i = 1, 5 do --immediately spawned for j = 1, 5 do crates[count] = display.newImage( "crate.png", 300 + (i\*90), -5000 - (j\*120) ) physics.addBody(crates[count], {density = 0.1, friction = 1, bounce = 0}) count = count+1 end end function finalize( self ) Runtime:removeEventListener( "enterFrame", self.label ) end function finalizeSpawned(self) Runtime:removeEventListener("enterFrame", self.newlabels) end function createCrate(x,y)--user spawns spawnedCrate = display.newImageRect("crate.png", 90, 90) spawnedCrate.x = x spawnedCrate.y = y physics.addBody(spawnedCrate, "dynamic", {density = 0.1, friction = 1, bounce = 0}) spawnedCrateCount = spawnedCrateCount + 1 spawnedCrate.id = #crates + spawnedCrateCount print(spawnedCrate.id) newlabels = display.newText(crate.parent, #crates + spawnedCrateCount, 0, 0) newlabels.crate = spawnedCrate newlabels.enterFrame = enterFrame Runtime:addEventListener("enterFrame", newlabels) end function lostCrates() if spawnedCrate == nil then --nothing else if (spawnedCrate.x \< 0 or spawnedCrate.x \> display.actualContentWidth) then print("Crate Number "..spawnedCrate.id.. " was lost") spawnedCrate = nil end end end Runtime:addEventListener("enterFrame", lostCrates) function createLabels() for x = 1, #crates do crate = crates[x] crate.id = x print(crate.id) -- https://docs.coronalabs.com/api/library/display/newText.html#syntax-legacy label = display.newText( crate.parent, x, 0, 0 ) label.crate = crate label.enterFrame = enterFrame Runtime:addEventListener( "enterFrame", label ) -- to help you remove the label later when removing the crate crate.label = label crate.finalize = finalize crate:addEventListener( "finalize" ) end end createLabels()

EDIT: Ignore this for now, and please focus on the question below, I will ask this once the comment under this is solved.

Right now, I am trying to make this function work: 

function crateLost() if (crate.x \< 0 or crate.x \> display.actualContentWidth + 75) then --line 137 crate.label = label crate.finalize = finalize crate:addEventListener( "finalize" ) print("Crate number "..crate.id.. "was lost") crate = nil end end

But the printing does not work, it just gives me an error the says:  Attempt to index upvalue ‘crate’ (a nil value), and it points to line 137, which is labeled in the code.

I don’t know why this is happening. this is the code that creates my crates:

for i = 1, 5 do for j = 1, 5 do crates[count] = display.newImage( "crate.png", 300 + (i\*90), -5000 - (j\*120) ) physics.addBody(crates[count], {density = 0.1, friction = 1, bounce = 0}) count = count+1 end end

And this is the code that creates labels for those crates:

function createLabels() for a = 1, #crates do crate = crates[a] crate.id = a --print(crate.id) -- https://docs.coronalabs.com/api/library/display/newText.html#syntax-legacy label = display.newText( crate.parent, a, 0, 0 ) label.crate = crate label.enterFrame = enterFrame Runtime:addEventListener( "enterFrame", label ) -- to help you remove the label later when removing the crate crate.label = label crate.finalize = finalize crate:addEventListener( "finalize" ) end end

So does anyone know why this is happening? 

EDIT: This was happening because I niled out the crate, but now it only prints that one crate was lost and does this forever. It repeats it forever because it is on an enterFrame event listener. ( Runtime:addEventListener(“enterFrame”) )  I understand why this happens, but how can I fix it.

Right now, I am trying to make this function work: 

function crateLost() if (crate.x \< 0 or crate.x \> display.actualContentWidth + 75) then --line 137 crate.label = label crate.finalize = finalize crate:addEventListener( "finalize" ) print("Crate number "..crate.id.. "was lost") crate = nil end end

But the printing does not work, it just gives me an error the says:  Attempt to index upvalue ‘crate’ (a nil value), and it points to line 137, which is labeled in the code.

I don’t know why this is happening. this is the code that creates my crates:

for i = 1, 5 do for j = 1, 5 do crates[count] = display.newImage( "crate.png", 300 + (i\*90), -5000 - (j\*120) ) physics.addBody(crates[count], {density = 0.1, friction = 1, bounce = 0}) count = count+1 end end

And this is the code that creates labels for those crates:

function createLabels() for a = 1, #crates do crate = crates[a] crate.id = a --print(crate.id) -- https://docs.coronalabs.com/api/library/display/newText.html#syntax-legacy label = display.newText( crate.parent, a, 0, 0 ) label.crate = crate label.enterFrame = enterFrame Runtime:addEventListener( "enterFrame", label ) -- to help you remove the label later when removing the crate crate.label = label crate.finalize = finalize crate:addEventListener( "finalize" ) end end

So does anyone know why this is happening? 

EDIT: This was happening because I niled out the crate, but now it only prints that one crate was lost and does this forever. It repeats it forever because it is on an enterFrame event listener. ( Runtime:addEventListener(“enterFrame”) )  I understand why this happens, but how can I fix it.