Global Variable Help pleezzz :((((

I have this code where i am trying to add a global variable by 1 so it can display a picture but it is not working. :frowning:

init_troops.lua (globals are shown):

\_G.troops = {  
  
 blueWarrior = 1,  
 redWarrior = 1,  
 blueTank = 1,  
 redTank = 1,  
 bluePlane = 1,  
 redPlane = 1,  
  
}  

troops.lua(where globals are added):

local function pressWarrior (event)  
if event.phase == "ended" then  
\_G.troops['blueWarrior'] = \_G.troops['blueWarrior'] + 1  
end  
end  
  
warriorButton:addEventListener ("touch", pressWarrior)  

play.lua(where variable is read for picture to be displayed):

if \_G.troops['blueWarrior'] == 1 then warrior = display.newImage('images/warrior.png') end [import]uid: 34097 topic_id: 16875 reply_id: 316875[/import]

if it helps I’m using the director class [import]uid: 34097 topic_id: 16875 reply_id: 63459[/import]

Your accessing your table wrong.

The way you have your tables set up you need to access there contents like :

_G.troops.blueWarrior
_G.troops[“blueWarrior”] is invalid as thats not how you have your table set up. [import]uid: 84637 topic_id: 16875 reply_id: 63595[/import]