Attempt to index field '?' a nil value error (a different one)

I had made a topic before about the same error, but this one is different. I measure the amount of lives the player has in game by displaying hearts and counting the lives. But sometimes a get the nil value error on line 365:

local function playerHit() character:setLinearVelocity(0, nil) composer.gotoScene("level1-Try-Again") timer.cancel(firstTimer) timer.cancel(secondTimer) timer.cancel(moveEnemiesTimer) lives[livesCount].alpha = 0 --Line 365 livesCount = livesCount - 1 if(livesCount \< 1) then onGameOver() end end

This is how I create and manage lives:

local lives = {} -- table that will hold the lives object local livesCount = 4 for i=1, livesCount do lives[i] = display.newImageRect("heart.png", 50, 50) lives[i].x = \_L + (i \* 65) - 25 lives[i].y = \_T + 50 end

Sincerely,

Alex

What that error more than likely is telling you, is that the object at that table location doesn’t exist. You might want to try printing out the value of “livesCount” before you try to alpha out that object, maybe even iterating through the “lives” table and identify which one you want to alpha/remove.

How would I iterate through the table?

Hi alex. You can try doing this
For i = 1,#lives do
Lives[i].alpha = 0
End
Hope that helps.
Because if you enter livesCount in lives table like this : lives[livesCount], then it will just enter the value of livesCount in lives table…for example, if your value of livesCount is 5 and you perform lives[livesCount], then it indirectly means like this: lives[5]. And therefore it the code will be somewhat like this:
Lives[5].alpha = 0
which means you are setting 5th lives alpha to zero
If you want to set each of the lives to zero alpha then do this:
For i=1, #lives do
Lives[i].alpha = 0
End
Hope that seems to work well and if there is something i went wrong with in my explanation, i am sorry :slight_smile:

EDIT:

Above code was for reducing alpha of each life.

But i think you want to reduce alpha of life.

So this error occurs because livesCount variable is nil i.e it’s value is zero…an you haven’t created lives[0], so therefore getting this error.

you could avoid this :

local function playerHit() character:setLinearVelocity(0, nil) timer.cancel(firstTimer) timer.cancel(secondTimer) timer.cancel(moveEnemiesTimer) -- you can try nilling out timers to improve performance firstTimer = nil secondTimer = nil moveEnemiesTimer = nil -- this will check if your livesCount is greater than zero or else if it is zero, it will give you error if livesCount \>= 1 then lives[livesCount].alpha = 0 livesCount = livesCount - 1 composer.gotoScene("level1-Try-Again") elseif livesCount \< 1 then gameOver() end end

This is working well, but occasionally the player loses two lives instead of one, could this be in the collision function?

I apologize that it took a while to respond, school just started.

This is not working very well for me, there appears to be heart images left but the player still has lives, and the reduction of lives seems to be occuring twice. 

This is my playerHit():

local function playerHit() timer.cancel(firstTimer) timer.cancel(secondTimer) timer.cancel(moveEnemiesTimer) if livesCount \>= 1 then lives[livesCount].alpha = 0 livesCount = livesCount - 1 print(livesCount) character:setLinearVelocity(0, nil) composer.gotoScene("level1-Try-Again") elseif(livesCount \< 1) then onGameOver() end end

Remove collision listener when going to level 1 try again

Try using a print statemnt in collision function to check whether it is executed two times when coming back to play scene from try again lvl 1 scene

It executes twice when going to level 1 try again. Also, the collision function is removed on scene:hide.

local function playerHit()
timer.cancel(firstTimer)
timer.cancel(secondTimer)
timer.cancel(moveEnemiesTimer)
if livesCount >= 1 then
lives[livesCount].alpha = 0
livesCount = livesCount - 1
print(livesCount)

Runtime:removeEventListener(“collision”,collisionlistener)
character:setLinearVelocity(0, nil)
composer.gotoScene(“level1-Try-Again”)
elseif(livesCount < 1) then
onGameOver()
end
end

The error still occurs, this is what is printed out in the console:

3 2 1 0

The 3 and 2 occurred at the same time.

Can you please zip your game folder and send it to me???By using github or sending me email

hcomb14@gmail.com

I have sent you an email.

Please fix the error that arises when going to level 1 scene

C:\Users\admin\Desktop\Margery Jumps\level1.lua:76: Incorrect number of frames (w,h) = (167,141) with border (0) in texture (w,h) = (500,424). Failed after frame 7 out of 9.

13:09:46.265  stack traceback:

13:09:46.265   [C]: in function ‘error’

13:09:46.265   ?: in function ‘gotoScene’

13:09:46.265   C:\Users\admin\Desktop\Margery Jumps\levelselect.lua:42: in function ‘_onEvent’

13:09:46.265   ?: in function ‘?’

13:09:46.265   ?: in function <?:449>

13:09:46.265   ?: in function <?:169>

I have sent the new email.

That error still occurs

it is better advised to have number of columns that can be divided by the width of the image easily… There should be no decimals like you have placed in following code

local beetleSheetData = {width=166.67, height=106, numFrames=10, sheetContentWidth=500, sheetContentHeight=424}

Okay so figured out your problem

It lies here in this piece of code:

physics.addBody(enemy, "dynamic", physicsData:get("beetle")) physics.addBody(enemy2, "dynamic", physicsData:get("vulture")) physics.addBody(enemy3, "dynamic", physicsData:get("scorpion")) physics.addBody(enemy4, "dynamic", physicsData:get("bee")

Due to this, your collision function is been called twice or maybe thrice

Instead of using complex shapes, i replaced them by using normal shapes

physics.addBody(enemy, "dynamic") physics.addBody(enemy2, "dynamic") physics.addBody(enemy3, "dynamic") physics.addBody(enemy4, "dynamic")

This code worked for me !! This code reduces one life when colliding with enemy…

See this guide:

https://docs.coronalabs.com/daily/guide/physics/physicsBodies/index.html

it will help you in defining shapes for your physics bodies

Hope this helped you :smiley:

Yes, but using normal shapes makes it look very unrealistic, it appears as if the character is colliding with thin air.