"attempt to index global 'self' (a nil value)

I’m new to Corona so please go easy.

I’m making a dice game for a class and the whole program of betting, rolling the dice and finding out if you won or lost works fine. However, when I go to reset the game after you have one the game, I get an error.

I created a “Play Again” button after you have found out if you have won or lost the game and when tapped on to restart the process via “main()”, it gives me this error. 

My code:

local function placeBet (self,event)
    bet5.alpha=1
    bet10.alpha=1
    bet15.alpha=1
    bet20.alpha=1
    self.alpha=0.7***
    bet=0
    bet= self.value
    betText.text=“Bet $”…self.value
    print(bet)
    moneyBetMade= true
end

That is the code for the function you’d be placing a bet on. bet5, bet10, bet15, and bet20 are all buttons to be tapped on and when tapped on, the button’s alpha dimes down to show you which one you have tapped on. *** is where the error occurs.

Any other code needed, please just ask! Please remember that I am new to this and it could be a simple mistake! Thanks in advance!

How are you calling that function? The variable self points to the parent object (a table in Lua). And you don’t usually pass it explicitly. For example:

local myObject = {} myObject.myVar = 5 local function myObject:myMethod() print(self.myVar) -- prints 5 end

In that code self  is a reference to myObject. So, for your function to work you’ll have to be passing that self parameter manually. Like so:

placeBet(whateverThisIs, event)

You may come from Python where you have to specify the self parameter but in Lua you don’t have to.

So, the complete solution would be something like this:

-- playAgainButton is the button that triggers this function local function playAgainButton:placeBet() -- self now points to playAgainButton bet5.alpha=1 bet10.alpha=1 bet15.alpha=1 bet20.alpha=1 self.alpha=0.7\*\*\* bet=0 bet= self.value betText.text="Bet $"..self.value print(bet) moneyBetMade= true end -- Maybe you're doing this already but you have to subscribe that function -- to the playAgainButton playAgainButton:addListener("tap", playAgainButton)

How are you calling that function? The variable self points to the parent object (a table in Lua). And you don’t usually pass it explicitly. For example:

local myObject = {} myObject.myVar = 5 local function myObject:myMethod() print(self.myVar) -- prints 5 end

In that code self  is a reference to myObject. So, for your function to work you’ll have to be passing that self parameter manually. Like so:

placeBet(whateverThisIs, event)

You may come from Python where you have to specify the self parameter but in Lua you don’t have to.

So, the complete solution would be something like this:

-- playAgainButton is the button that triggers this function local function playAgainButton:placeBet() -- self now points to playAgainButton bet5.alpha=1 bet10.alpha=1 bet15.alpha=1 bet20.alpha=1 self.alpha=0.7\*\*\* bet=0 bet= self.value betText.text="Bet $"..self.value print(bet) moneyBetMade= true end -- Maybe you're doing this already but you have to subscribe that function -- to the playAgainButton playAgainButton:addListener("tap", playAgainButton)