How to destroy a ADMOB ad

when i use the underneath code in my main.lua file it displays the ad as i want it to be. but when i add "ads.hide()" (the ad stay on every scene) in the "exitScene" scene section i get this error in the terminal " attempt to index global 'ads' (a nil value)" which i understand as the ads don't display in the simulator but when i open the app op my phone (galaxy s4) non of the buttons respond, and it just stay on the main.lua file/scene

local provider = "admob"
local appID = "**********"
local ads = require "ads"

local screenGroup = self.view
local statusText = display.newText( "", 0, 0, native.systemFontBold, 22 )
statusText:setTextColor( 255 )
statusText:setReferencePoint( display.CenterReferencePoint )
statusText.x, statusText.y = display.contentWidth * 0.5, 160

local showAd
local function adListener( event )

    local msg = event.response
    print("Message received from the ads library: ", msg)

    if event.isError then
        statusText:setTextColor( 255, 0, 0 )
        statusText.text = "Error Loading Ad"
        statusText.x = display.contentWidth * 0.5
        local screenGroup = self.view
        showAd( "banner" )


if appID then
    ads.init( provider, appID, adListener )

local sysModel = system.getInfo("model")
local sysEnv = system.getInfo("environment")

    showAd = function( adType )
    local screenGroup = self.view
    local adX, adY = display.screenOriginX, 400
    statusText.text = "" adType, { x=adX, y=adY } )

if sysEnv == "simulator" then
local screenGroup = self.view
    showAd( "banner" )

Hi how can i close or destroy a "admob" ad on screen change ?


You need to require ads in every scene you create

Add this line to every Lua file that are using the ads plugin

local ads = require("ads")

ads.hide( )

you can insert above into any event listener or something else.

You need to call this function in every screen.

if ads then



Note: When you are generating dynamic ads then there are time duration between the ads. So if the ads is on the screen it will hide otherwise not. But when you not check the if condition and move to next scene the ads will load again.

Need Your Help

manageable way to handle exceptions in java

java spring exception ibatis dao

I am trying to come up with a manageable way to handle exceptions in a DAO. Typically a method in my DAO looks like this:

Getting max value from a list in SML

list recursion sml

I'm currently studying SML and I'm having a hard time understanding the code below