Help with Lua Sarven Sentry?


Doing the Sarven Sentry level where you have to build different types of structures depending on flag color. I have written this code:

    local flagGreen = self:findFlag("green")
    local flagBlack = self:findFlag("black")
    local flagViolet = self:findFlag("violet")
    if flagGreen then
        self:buildXY("fence", flagGreen.pos.x, flagGreen.pos.y)
    elseif flagBlack then
        self:buildXY("fire-trap", flagBlack.pos.x, flagBlack.pos.y)
        self:moveXY(flagViolet.pos.x, flagViolet.pos.y)

But when I try to run it, the game says I have to fix my code, specifically this: Line 13: attempt to index ‘flagViolet’ (a nil value). I’ve clearly defined flagViolet as a local variable equating to self:findFlag(“violet”). Help, please? :confused:

Edit: Ten seconds later… Simply changed the end else to an elseif flagViolet and it works now. I don’t really see why, but okay :stuck_out_tongue:


Your problem was that your program automatically assumes there is a violet flag if there is no green or black flag. This is not necessarily true. You needed to check for a violet flag.