Upvalue Error and Listener Help

Hello everyone,

I am getting a strange error with the game I am working on. I am trying to get shooting and collisions to work currently, and I’m also having trouble with the listeners. I’ve attached my code in a .txt. 

The error I’m getting occurs in my shoot function, and reads: 

"attempt to index upvalue ‘player’ (a nil value)

In the shoot function, line 205 is as follows:

bullet.x = player.x

Any help would be appreciated. Let me know if any more information is needed. 

Where are you calling your spawnPlayer() function? Your shoot function is getting called when your player object is nil.

I call spawnPlayer() within the transition.to in my removeTitleScreen function. Should I be calling it somewhere else? 

I think you might be getting caught by the fact that every touch of the screen that is short enough to be considered a “tap” event triggers both a “tap” and a “touch” event.  You set up a “tap” event on the global Runtime listener.  Your splash screen and howto screen are capturing touch events and because the tap event is also being fired, it looks like your shoot is getting called before you spawn your player.  I would move the addEventListener() for the tap to right before you call the transition.to  or move it inside the transition.to’s onComplete handler.

I moved the listener to just before the transition.to, but the error still occurred, and I can’t seem to get the code syntax correct inside the transition.to onComplete handler. What would that line of code look like? Also, should I perhaps make it something other than a Runtime listener?  

Quickly looking at the file…

  1. You should pre-declare ‘player’ at the beginning (or near).

    local player – this is a pre-declare

That way, if anything needs to call player, it will always be in scope.

Just gave it a shot, but the error is still happening. 

Maybe I am calling shoot in the wrong location. Should I try moving it to another function, such as spawnPlayer()? Would that give me the local player that I am spawning in spawnPlayer() in the shoot() function?
 

Well if it’s still an error, that at least means its calling your function at some point where player doesn’t exist. 

Something you might want to try to narrow things down; make a new variable at the start (ie: ‘playerReady’). Set it to true when you create the player. Then edit shoot() to only do something if playerReady. If there’s still a problem, that means it’s probably because at some point the player variable is being destroyed or overridden…

Try making it a touch and not a tap

I did the playerReady thing and it got rid of my error and I can now shoot. Thanks to both of you, though, you both helped me a lot. 

Where are you calling your spawnPlayer() function? Your shoot function is getting called when your player object is nil.

I call spawnPlayer() within the transition.to in my removeTitleScreen function. Should I be calling it somewhere else? 

I think you might be getting caught by the fact that every touch of the screen that is short enough to be considered a “tap” event triggers both a “tap” and a “touch” event.  You set up a “tap” event on the global Runtime listener.  Your splash screen and howto screen are capturing touch events and because the tap event is also being fired, it looks like your shoot is getting called before you spawn your player.  I would move the addEventListener() for the tap to right before you call the transition.to  or move it inside the transition.to’s onComplete handler.

I moved the listener to just before the transition.to, but the error still occurred, and I can’t seem to get the code syntax correct inside the transition.to onComplete handler. What would that line of code look like? Also, should I perhaps make it something other than a Runtime listener?  

Quickly looking at the file…

  1. You should pre-declare ‘player’ at the beginning (or near).

    local player – this is a pre-declare

That way, if anything needs to call player, it will always be in scope.

Just gave it a shot, but the error is still happening. 

Maybe I am calling shoot in the wrong location. Should I try moving it to another function, such as spawnPlayer()? Would that give me the local player that I am spawning in spawnPlayer() in the shoot() function?
 

Well if it’s still an error, that at least means its calling your function at some point where player doesn’t exist. 

Something you might want to try to narrow things down; make a new variable at the start (ie: ‘playerReady’). Set it to true when you create the player. Then edit shoot() to only do something if playerReady. If there’s still a problem, that means it’s probably because at some point the player variable is being destroyed or overridden…

Try making it a touch and not a tap

I did the playerReady thing and it got rid of my error and I can now shoot. Thanks to both of you, though, you both helped me a lot.