Attempt to index global 'object' (a nil value)

So this is the error I have been getting:

Game.lua:66: attempt to index global 'Spears' (a nil value) stack traceback:                          Game.lua:66: in function '\_listener'

and this is some of the code, showing where the error happens:

    local Spears = {}      local SpearsTimer      local SpearsCounter = 1     local delayTimer     local removeListeners end end local field = display.newCircle( 0, 0, 0 ) ; field.alpha = 0.3 field.name = "field" field.x = display.contentCenterX ; field.y = display.contentCenterY physics.addBody( field, "static", { isSensor=true, radius=320 } )     local spawnSpears = function()          local Fall = math.random(display.contentWidth \* -0.2, display.contentWidth \* 1.2)          Spears[SpearsCounter] = display.newImageRect( "Spear.png", 15, 50 )          Spears[SpearsCounter].x = Fall          Spears[SpearsCounter].y = -200           physics.addBody( Spears[SpearsCounter], "dynamic", {bounce = 0} )          --Spears[SpearsCounter].collision = onCollision          --Spears[SpearsCounter]:addEventListener( "collision", Spears[SpearsCounter] )          transition.to( Spears[SpearsCounter], { rotation = Spears[SpearsCounter].rotation+720, time=15000, onComplete=spinImage } )          Spears[SpearsCounter].gravityScale = 0.5          sceneGroup:insert(Spears[SpearsCounter])         group:insert(Spears[SpearsCounter])         SpearsCounter = SpearsCounter + 1     end     SpearsTimer = timer.performWithDelay( 5000, spawnSpears, -1 )

The Error points to line 66, which is this line of code:

 Spears[SpearsCounter] = display.newImageRect( "Spear.png", 15, 50 ) 

So what am I doing wrong?

Spears is not visible at that point and Lua thinks you must be referring to a global.  The error message is a guess by Lua.

The main issue is your scope and visibility is broken for ‘Spears’.

I see two end statements after ‘Spears’, which tell me you defined it in a function or method.  Thus it is not visible to the subsequent one.

The following code would produce a similar warning:

local function one() local Spears = {}    Spears[1] = 10 end local function two() Spears[10] = 20 end two()

So if you don’t mind me asking, how would I fix this issue?

Put the variable at file-level scope.  That is one way.  It hard to answer though not knowing the structure of your project or file(s) (no need to dump them.)

Just understand that the problem is scope and fix it from there. i.e. Once you’re solid on scope and visibility rules, you won’t need me to tell you the answer. You’ll see it.

For now:

If you 're going to reference the table in a bunch of functions in the file, put the declaration and definition at the top of the file (or at least above all the functions/code that reference it.

Note: This would fix the example I gave:

local Spears = {} local function one() Spears[1] = 10 end local function two() Spears[10] = 20 end two()

Here, let me say this first. What I am trying to do, is to make an object, that I just named Spear, randomly spawn and go to the center of the screen. So what I did was add physics and Radial Gravity. But as it looks it is harder than I thought( But keep in mind that I have another test one that works, with almost the same code, but this code does not work for some reason…). Is there any easier way to do this?

Like roaminggamer said, you have a scope issue… One function cant see the other to call the 1st one…

Spears is not visible at that point and Lua thinks you must be referring to a global.  The error message is a guess by Lua.

The main issue is your scope and visibility is broken for ‘Spears’.

I see two end statements after ‘Spears’, which tell me you defined it in a function or method.  Thus it is not visible to the subsequent one.

The following code would produce a similar warning:

local function one() local Spears = {}    Spears[1] = 10 end local function two() Spears[10] = 20 end two()

So if you don’t mind me asking, how would I fix this issue?

Put the variable at file-level scope.  That is one way.  It hard to answer though not knowing the structure of your project or file(s) (no need to dump them.)

Just understand that the problem is scope and fix it from there. i.e. Once you’re solid on scope and visibility rules, you won’t need me to tell you the answer. You’ll see it.

For now:

If you 're going to reference the table in a bunch of functions in the file, put the declaration and definition at the top of the file (or at least above all the functions/code that reference it.

Note: This would fix the example I gave:

local Spears = {} local function one() Spears[1] = 10 end local function two() Spears[10] = 20 end two()

Here, let me say this first. What I am trying to do, is to make an object, that I just named Spear, randomly spawn and go to the center of the screen. So what I did was add physics and Radial Gravity. But as it looks it is harder than I thought( But keep in mind that I have another test one that works, with almost the same code, but this code does not work for some reason…). Is there any easier way to do this?

Like roaminggamer said, you have a scope issue… One function cant see the other to call the 1st one…