How to have Ammo Upgrade?

I need help on this very important part of my game ok so this is what I’m doing is when a person plays a game they start with the game with 30 bullets. What I’m trying to do is when they go to the shop and buy upgrade bullets, now when they buy it they now can hold 60 bullets. This is how I have it setup

shop.lua

[code]module(…, package.seeall)

– Main function - MUST return a display.newGroup()
function new()
local localGroup = display.newGroup()

gameState = require(“GameState”)

if(gameState.money == nil) then
gameState.money = 0
–other init code can go here
end

local bankText2 = display.newText("", 160, 30,“Arial”,20)
bankText2.text = “$”… gameState.money
localGroup:insert(bankText2)

local box = display.newRect(160, 240, 30, 30)
localGroup:insert(box)

local box2 = display.newRect(10, 10, 30, 30)
localGroup:insert(box2)

local box3 = display.newRect(250, 160, 30, 30)
localGroup:insert(box3)

local box4 = display.newRect(300, 160, 30, 30)
localGroup:insert(box4)

local bankText = display.newText("", 160, 30,“Arial”,20)
localGroup:insert(bankText)

local highText = display.newText("", 0,0,“Arial”,22)
highText.x = display.contentWidth/2
highText.y = 100
localGroup:insert(highText)

local function change ()
if gameState.money > 59 then
gameState.money = gameState.money - 1
bankText2.text = “$”… gameState.money
saveFile(“twinb.txt”, 1)
saveFile(“extra.txt”, 2) — Right here is were it changes
print(“Bought!”)
elseif gameState.money <= 59 then
print(“Not enough!”)
end
end
box:addEventListener(“tap”, change)

return localGroup
end[/code]

game.lua

[code] local function powertwin2 ()
extrabull = loadFile (“extra.txt”)
if extrabull == “empty” then
extrabull = 1
saveFile(“extra.txt”, extrabull)
end
end
powertwin2()

local function bulletsgood()
if tonumber(extrabull) == 1 then
local limits = 30
local limitsbullet = display.newText(“Ammo:30”, 30, 30,“Arial”, 18)
localGroup:insert(limitsbullet)
end
end
Runtime:addEventListener( “enterFrame”, bulletsgood)
local function bulletsgood2()
if tonumber(extrabull) == 2 then
local limits = 60
local limitsbullet = display.newText(“Ammo:60”, 30, 30,“Arial”, 18)
localGroup:insert(limitsbullet)
end
end
bulletsgood2()
local function shoot (event) – This function is when they shoot
if limits == 0 then
great = true
end
if event.phase == “release” then
if great == false then
if tonumber(twinb) == 1 then
local bullet = display.newImageRect(“bullet.png”, 10, 10)
bullet.x = heli.x + 40
bullet.y = heli.y
bullet.hit = “bullet”
bullet.isFixedRotation = true
transition.to (bullet, {time = 2000, x=550})
physics.addBody (bullet, {bounce=0.6})
limits = limits - 1
limitsbullet.text = “Ammo:” …limits
localGroup:insert(bullet)
bullet:addEventListener(“collision”, bullet)
function bullet:collision (event)
if event.other.hit ~= “heli” then
bullet:removeSelf()
end
end
elseif tonumber(twinb) == 2 then
local twin = display.newImageRect(“bullet.png”, 10, 10)
twin.x = heli.x + 40
twin.y = heli.y
twin.hit = “twin”
twin.isFixedRotation = true
transition.to (twin, {time = 2000, x=550})
physics.addBody (twin, {bounce=0.6})
localGroup:insert(twin)
local twin2 = display.newImageRect(“bullet.png”, 10, 10)
twin2.x = heli.x + 40
twin2.y = heli.y + 15
twin2.hit = “twin”
twin2.isFixedRotation = true
transition.to (twin2, {time = 2000, x=550})
physics.addBody (twin2, {bounce=0.6})
localGroup:insert(twin2)
twin:addEventListener(“collision”, twin)
function twin:collision (event)
if event.other.hit ~= “heli” then
twin:removeSelf()
twin2:removeSelf()
end
end
elseif tonumber(twinb) == 3 then
local bullet3 = display.newImageRect(“bullet.png”, 20, 20)
bullet3.x = heli.x + 40
bullet3.y = heli.y
bullet3.hit = “bullet3”
bullet3.isFixedRotation = true
transition.to (bullet3, {time = 2000, x=550})
physics.addBody (bullet3, {bounce=0.6})
localGroup:insert(bullet3)
bullet:addEventListener(“collision”, bullet3)
function bullet:collision (event)
if event.other.hit ~= “heli” then
bullet3:removeSelf()
end
end

end
end
end
end

ButtonA = ui.newButton{
default = “buttonoverA.png”,
over = “buttonA.png”,
x = 360,
y = 280,
onEvent = shoot,
}
localGroup:insert(ButtonA)
[/code]

I use ego. I’m trying to have this work because is very important to my game [import]uid: 17058 topic_id: 22814 reply_id: 322814[/import]

You have almost no comments.

I can see you are saving and loading the value fine, what exactly is the issue you’re having? [import]uid: 52491 topic_id: 22814 reply_id: 91121[/import]

Ok basically what I’m doing is when the limits are loaded which are the bullets is either is 30 or 60. So when one of those are loaded the player can either shoot 30 or 60 bullets but very time I do that it does not work and I get an error [import]uid: 17058 topic_id: 22814 reply_id: 91124[/import]

ok I edited again [import]uid: 17058 topic_id: 22814 reply_id: 91125[/import]

OK, well, what’s the error? What line? That’s really important to mention when you post your code :wink: [import]uid: 52491 topic_id: 22814 reply_id: 91204[/import]

@peach ok the error is at lines 44, and 45 in terminal it would say

“attempt to perform arithmetic on global ‘limits’ (a nil value)”

The reason I have it in function the # of bullets is because I want to the person to have different amounts of ammo. Like when they go to the shop they upgrade there ammo from 30 to 60. So this is what I’m trying to do but I’m getting this error and this is the closets I can try.

The important lines are from lines 10-17 because what I’m trying to do again is call 30 bullets if extrabull == 2. So with that if somone clicks to buy extra bullets it extrabull would be == 3 and they would have more bullets and etc… when they buy more upgrades.

If your confuse of what I said feel free to ask me questions [import]uid: 17058 topic_id: 22814 reply_id: 91242[/import]

It’s because your declaring “limits” in a function so it is local only to that function. Your next function can’t see it, hence the error.

You need to do something like this (outside of a function, at the top of your code somewhere)

  
--set limits initial declaration  
local limits = 0  
  

Then whenever you need to access it again just do

limits = 30 --or whatever [import]uid: 84637 topic_id: 22814 reply_id: 91250[/import]

@Danny so for

limits == 30 

I can access it by doing a function would that be right? [import]uid: 17058 topic_id: 22814 reply_id: 91278[/import]

If you want to check if its equal to 30 then you can do that yeah.
basically you can do it how you already have it implemented, that doesn’t need to change.

You just need to declare the limits variable outside of the functions so all functions needed can see it and access it.

If you don’t your going to get “attempt to access “whatever” a nil value”. [import]uid: 84637 topic_id: 22814 reply_id: 91279[/import]