Runtime Error: 'end; expected (to close 'if' at line 119) near <eof>

I am aware this is probably a silly mistake but I have been staring at my code fr an hour trying to fix this error, can anyone help me.



– scene1.lua


– Your code here
local composer = require(“composer”)

local scene = composer.newScene(“MainGame”)

function scene:create( event )

local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
local background = display.newImageRect( "background.jpg", 360, 570 )

background.x = display.contentCenterX
background.y = display.contentCenterY
sceneGroup:insert( background )

local backgroundDamage = display.newImageRect( “backgrounddamage.jpg”, 360, 570 )
backgroundDamage.x = display.contentCenterX
backgroundDamage.y = display.contentCenterY
backgroundDamage.alpha = 0
sceneGroup:insert( backgroundDamage )

local monster = display.newImageRect( “wolf.png”, 315, 240)
monster.x = display.contentCenterX
monster.y = display.contentCenterY
sceneGroup:insert( monster )

local score = 0
local scoreDisplay = display.newText( "Score: " … score, display.contentCenterX, 10, native.systemFont, 40)
scoreDisplay:setFillColor( 255, 255, 255 )
sceneGroup:insert( scoreDisplay )

local playerHealthNumber = 100

local monsterHealth = display.newText( “100”, display.contentCenterX, 100, native.systemFont, 40)
monsterHealth:setFillColor( 255, 255, 255 )
sceneGroup:insert( monsterHealth )

local playerHealthBarRed = display.newRect( display.contentCenterX, 498, playerHealthNumber * 2.5, 40 )
playerHealthBarRed:setFillColor( 255, 0, 0 )
sceneGroup:insert( playerHealthBarRed )

local playerHealthBar = display.newRect( display.contentCenterX, 498, playerHealthNumber * 2.5, 40 )
playerHealthBar:setFillColor( 255, 255, 255 )
sceneGroup:insert( playerHealthBar )

local playerHealth = display.newText( “100”, display.contentCenterX, 500, native.systemFont, 40)
playerHealth:setFillColor( 0, 0, 0 )
sceneGroup:insert( playerHealth)

local sword = display.newImageRect( “swordbutton.png”, 50, 50)
sword.x = display.contentCenterX - 75
sword.y = display.contentCenterY + 200
sceneGroup:insert( sword )

local sheild = display.newImageRect( “sheildbutton.png”, 50, 50)
sheild.x = display.contentCenterX + 75
sheild.y = display.contentCenterY + 200
sceneGroup:insert( sheild )

local magic = display.newImageRect( “magicbutton.png”, 50, 50)
magic.x = display.contentCenterX
magic.y = display.contentCenterY + 200
sceneGroup:insert( magic )

local magicAni = display.newImageRect( “fire.png”, 400, 400)
magicAni.x = display.contentCenterX
magicAni.y = display.contentCenterY - 30
magicAni.alpha = 0
sceneGroup:insert( magicAni )

local sheildAni = display.newImageRect( “sheild.png”, 200, 200)
sheildAni.x = display.contentCenterX
sheildAni.y = display.contentCenterY
sheildAni.alpha = 0
sceneGroup:insert( sheildAni )

local magicNumber = 10
local sheildNumber = 10
local swordDamage = 10

local magicUses = display.newRect( 185, 460, 20, 20 )
magicUses:setFillColor( 255, 255, 0 )
sceneGroup:insert( magicUses )

local magicUsesRemaining = display.newText( magicNumber, 185, 460, native.systemFont, 20)
magicUsesRemaining:setFillColor( 0, 0, 0 )
sceneGroup:insert( magicUsesRemaining )

local sheildUses = display.newRect( 260, 460, 20, 20 )
sheildUses:setFillColor( 255, 255, 0 )
sceneGroup:insert( sheildUses )

local sheildUsesRemaining = display.newText( sheildNumber, 260, 460, native.systemFont, 20)
sheildUsesRemaining:setFillColor( 0, 0, 0 )
sceneGroup:insert( sheildUsesRemaining )

local monsterTable = { ‘slime’ , ‘wolf’ }

local playerTurn = true
local monsterType = “slime”
local monsterMove = 0
local monsterDefeated = false
composer.setVariable( “shopChoice”, “none” )


local function shop()
local function checkLevel()
if (score == 5) then
table.insert(monsterTable, “minotaur”)
end
end

 	checkLevel()
 	if (shopChoice == "magic") then
      magicNumber = 10    
      magicUsesRemaining.text = tostring(magicNumber)
  else if (shopChoice == "sheild") then
      sheildNumber = 10
      sheildUsesRemaining.text = tostring(sheildNumber)
  else if (shopChoice == "sword") then
      swordDamage = swordDamage + 5	    	
  else if (shopChoice == "potion") then
      if (playerHealthNumber > 50) then
          playerHealthNumber = 100
      else
          playerHealthNumber =  playerHealthNumber + 50
      end
      display.remove( playerHealthBar )
      display.remove( playerHealth )
      playerHealthBar = display.newRect( display.contentCenterX, 498, playerHealthNumber * 2.5, 40 )
      playerHealth = display.newText( playerHealthNumber, display.contentCenterX, 500, native.systemFont, 40)
      playerHealth:setFillColor( 0, 0, 0 )
  end

end

-----------------------------------------------------------------------------------------------

local function spawnMonster()
if (monsterType == “slime”) then
monster.fill = { type = “image”, filename = “slime.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
print(“monster spawned”)
elseif (monsterType == “minotaur”) then
monster.fill = { type = “image”, filename = “minotaur.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
elseif (monsterType == “wolf”) then
monster.fill = { type = “image”, filename = “wolf.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
end
end

local function spawnRandomMonster()
print(“spawnRandomMonster”)
monsterType = monsterTable[ math.random( #monsterTable ) ]
spawnMonster()
monster.alpha = 100
monsterHealth.alpha = 100
monsterMove = 0
if (monsterType == “slime”) then
monsterHealth.text = 100
elseif (monsterType == “minotaur”) then
monsterHealth.text = 150
elseif (monsterType == “wolf”) then
monsterHealth.text = 50
end
end

local function playerHealthCheck()
playerHealthNumber = tonumber(playerHealth.text)
display.remove( playerHealthBar )
display.remove( playerHealth )
playerHealthBar = display.newRect( display.contentCenterX, 498, playerHealthNumber * 2.5, 40 )
playerHealth = display.newText( playerHealthNumber, display.contentCenterX, 500, native.systemFont, 40)
playerHealth:setFillColor( 0, 0, 0 )
print(“playerHealthCheck”)
if (playerHealthNumber < 1) then
composer.setVariable( “finalScore”, score )
composer.gotoScene(“GameOver”)
end
end

local function monsterHealthCheck()
playerHealthCheck()
local monsterHealthNumber = tonumber(monsterHealth.text)
print(“monsterHealthCheck”)
if (monsterHealthNumber < 1) then
monsterDefeated = true
monster.alpha = 0
monsterHealth.alpha = 0
score = score + 1
display.remove( scoreDisplay )
scoreDisplay = display.newText( "Score: " … score, display.contentCenterX, 10, native.systemFont, 40)
timer.performWithDelay( 100, spawnRandomMonster )
if (score % 5) == 0 then
composer.gotoScene(“Shop”)
end
end
end

local function monsterMoveUpdate()
if (monsterMove == 4) then
monsterMove = monsterMove - 3
print(“check”)
elseif (monsterMove < 5) then
monsterMove = monsterMove + 1
end
end

local function sheildAnimation()
sheildAni.alpha = 0
end

local function magicAnimation()
magicAni.alpha = 0
end

local function playerDamageAnimation()
backgroundDamage.alpha = 0
end

local function updateMagicNumber()
print(“updateMagicNumber”)
magicNumber = magicNumber - 1
magicUsesRemaining.text = tostring(magicNumber)
end

local function updateSheildNumber()
print(“updateSheildNumber”)
sheildNumber = sheildNumber - 1
sheildUsesRemaining.text = tostring(sheildNumber)
end

local function tapSword()
monsterMoveUpdate()
if (monsterType == “slime”) then
monster.fill = { type = “image”, filename = “slimedamage.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
timer.performWithDelay( 300, spawnMonster )
if(monsterMove == 1 or monsterMove == 2) then
monsterHealth.text = monsterHealth.text - swordDamage
timer.performWithDelay( 300, monsterHealthCheck )
elseif(monsterMove == 3 or monsterMove == 4) then
monsterHealth.text = monsterHealth.text - swordDamage
playerHealth.text = playerHealth.text - 5
timer.performWithDelay( 300, playerDamageAnimation )
backgroundDamage.alpha = 100
timer.performWithDelay( 300, monsterHealthCheck )
else
end
elseif (monsterType == “minotaur”) then
monster.fill = { type = “image”, filename = “minotaurdamage.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
timer.performWithDelay( 300, spawnMonster )
if(monsterMove == 1 or monsterMove == 3) then
monsterHealth.text = monsterHealth.text - swordDamage
playerHealth.text = playerHealth.text - 15
timer.performWithDelay( 300, playerDamageAnimation )
backgroundDamage.alpha = 100
timer.performWithDelay( 300, monsterHealthCheck )
elseif(monsterMove == 2 or monsterMove == 4) then
monsterHealth.text = monsterHealth.text - swordDamage
timer.performWithDelay( 300, monsterHealthCheck )
else
end
elseif (monsterType == “wolf”) then
monster.fill = { type = “image”, filename = “wolfdamage.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
timer.performWithDelay( 300, spawnMonster )
if(monsterMove == 1 or monsterMove == 2 or monsterMove == 3) then
monsterHealth.text = monsterHealth.text - swordDamage
playerHealth.text = playerHealth.text - 5
timer.performWithDelay( 300, playerDamageAnimation )
backgroundDamage.alpha = 100
timer.performWithDelay( 300, monsterHealthCheck )
elseif(monsterMove == 4) then
monsterHealth.text = monsterHealth.text - swordDamage
timer.performWithDelay( 300, monsterHealthCheck )
else
end
end
end

local function tapMagic()
if magicNumber > 0 then
monsterMoveUpdate()
magicAni.alpha = 100
updateMagicNumber()
timer.performWithDelay( 300, magicAnimation )
if (monsterType == “slime”) then
monster.fill = { type = “image”, filename = “slimedamage.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
timer.performWithDelay( 300, spawnMonster )
if(monsterMove == 1 or monsterMove == 2) then
monsterHealth.text = monsterHealth.text - 20
timer.performWithDelay( 300, monsterHealthCheck )
elseif(monsterMove == 3 or monsterMove == 4) then
monsterHealth.text = monsterHealth.text - 20
playerHealth.text = playerHealth.text - 5
timer.performWithDelay( 300, playerDamageAnimation )
backgroundDamage.alpha = 100
timer.performWithDelay( 300, monsterHealthCheck )
else
end
elseif (monsterType == “minotaur”) then
monster.fill = { type = “image”, filename = “minotaurdamage.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
timer.performWithDelay( 300, spawnMonster )
if(monsterMove == 1 or monsterMove == 3) then
monsterHealth.text = monsterHealth.text - 20
playerHealth.text = playerHealth.text - 15
timer.performWithDelay( 300, playerDamageAnimation )
backgroundDamage.alpha = 100
timer.performWithDelay( 300, monsterHealthCheck )
elseif(monsterMove == 2 or monsterMove == 4) then
monsterHealth.text = monsterHealth.text - 20
timer.performWithDelay( 300, monsterHealthCheck )
else
end
elseif (monsterType == “wolf”) then
monster.fill = { type = “image”, filename = “wolfdamage.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
timer.performWithDelay( 300, spawnMonster )
if(monsterMove == 1 or monsterMove == 2 or monsterMove == 3) then
monsterHealth.text = monsterHealth.text - 20
playerHealth.text = playerHealth.text - 5
timer.performWithDelay( 300, playerDamageAnimation )
backgroundDamage.alpha = 100
timer.performWithDelay( 300, monsterHealthCheck )
elseif(monsterMove == 4) then
monsterHealth.text = monsterHealth.text - 20
timer.performWithDelay( 300, monsterHealthCheck )
else
end
end
end
end

local function tapSheild()
if sheildNumber > 0 then
updateSheildNumber()
if (monsterType == “slime” or monsterType == “minotaur” or monsterType == “wolf”) then
monsterMoveUpdate()
sheildAni.alpha = 100
timer.performWithDelay( 500, sheildAnimation )
end
end
end

sword:addEventListener( “tap”, tapSword )
magic:addEventListener( “tap”, tapMagic )
sheild:addEventListener( “tap”, tapSheild )

spawnRandomMonster()

end

function scene:show( event )
local sceneGroup = self.view
local phase = event.phase

if ( phase == “will” ) then
– any code placed here will run when the scene is still “off-screen”, but about to be displayed to the user. In many cases, this will be empty.
elseif ( phase == “did” ) then
– any code placed here will run as soon as the scene is displayed on screen. This is where you would start any animations, start playing background audio, start timers, etc.
end
end

function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase

if ( phase == “will” ) then
– any code placed here will run when the scene is still on screen, but is about to go off screen. This is where you would stop timers, audio, and animations that you created in the show event.
elseif ( phase == “did” ) then
– any code placed here will run as soon as the scene is no longer visible. In many cases, this will be empty.
end
end

function scene:destroy( event )

local sceneGroup = self.view

end


– Scene event function listeners


scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )


return scene

The error message says that you are missing an end. You have a conditional statement somewhere that you aren’t closing.

I see that you also have a few cases of else if in your code, but those don’t exist in Lua, you need to use elseif. Your error could be caused by one of these mistyped else if’s.

When posting code to the forums, or when writing code in general, you should use proper indentation. Errors like these are fairly easy to spot when you are given a line number and you can follow the indentation to see where the problem occurs. Without indentation, debugging can be quite a lot more tedious and difficult.

Just like XeduR said…

see attached image