Program sometimes works sometimes doesn't (solved)

I wrote this code for a game but it has been causing issues. When I run the program it sometimes works but sometimes it doesn’t respond. Is there a fault in my code?


-- terrain

-- Use this function to perform your initial setup
function setup()
    
    displayMode(FULLSCREEN)
    supportedOrientations(LANDSCAPE_ANY)
    
    blocks = {5,5,5,5,5,5,5,5, 5,5,5,5,3,3,3,5, 5,5,5,5,3,3,3,5, 5,5,5,5,3,3,3,5, 5,5,5,5,3,3,3,5, 5,5,5,5,3,3,3,5, 5,5,5,5,3,3,3,5,}
    --0=empty 1=brown 2=dirt 3=grass 4=plain 5=stone 6=wall 7=water 8=wood 
    
    tallBlocks = {4,4,4,4,4,4,4,4, 4,0,0,4,0,0,0,4, 4,0,0,4,0,0,0,4, 4,0,0,4,0,0,0,4, 4,0,0,4,0,0,0,4, 4,0,0,0,0,0,0,4, 4,0,0,0,0,0,0,4,}
    --0=empty 1=stone 2=doorClosed 3=doorOpen 4=wall 5=window
    
    --objectBlocks = {[15] = 3, [14] = 2}
    objectBlocks = {0,0,0,0,0,0,0,0, 0,0,0,0,0,2,3,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,3,0,0}
    --0=empty 1=rock 2=smallTree 3=tallTree 4=uglyTree
    
    interactiveBlocks = {[11] = 1, [10] = 5, [13] = 4}
    --interactiveBlocks[11] = 1
    --0=empty 1=blueGem 2=greenGem 3=orangeGem 4=key 5=chestClosed 6=chestOpen
    
    player = {[23] = 1}
    -- 0=notThere 1=there 
    x = 100
    y = 400
    spsq = 23
    t = 0
    posChange = 0
    time = 0
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(0, 0, 0, 255)
    spriteMode(CORNER)
    x = 50
    y = HEIGHT - 220
    n = 1
    
    sprite("Cargo Bot:Command Grab", WIDTH - 75, 400, 50, 54) --down button
    rotate(180)
    spriteMode(CENTER)
    sprite("Cargo Bot:Command Grab", 0 - (WIDTH - 50), 0 - 525)--up button
    rotate(180)
    spriteMode(CORNER)
    sprite("Cargo Bot:Command Left", WIDTH - 75, 600)--left button
    sprite("Cargo Bot:Command Right", WIDTH - 75, 700) --right button
    
    for i = 1, #blocks do
        
        
        if blocks[i] == 0 then --draws the ground:
        
        elseif blocks[i] == 1 then
            sprite("Planet Cute:Brown Block", x, y)
        elseif blocks[i] == 2 then
            sprite("Planet Cute:Dirt Block", x, y)
        elseif blocks[i] == 3 then
            sprite("Planet Cute:Grass Block", x, y)
        elseif blocks[i] == 4 then
            sprite("Planet Cute:Plain Block", x, y)
        elseif blocks[i] == 5 then
            sprite("Planet Cute:Stone Block", x, y)
        elseif blocks[i] == 6 then
            sprite("Planet Cute:Wall Block", x, y)
        elseif blocks[i] == 7 then
            sprite("Planet Cute:Water Block", x, y)
        elseif blocks[i] == 8 then
            sprite("Planet Cute:Wood Block", x, y)
        end
        
        
        y = y + 40
        if tallBlocks [i] == 0 then --draws walls:
                
        elseif tallBlocks [i] == 1 then
            sprite("Planet Cute:Stone Block Tall",x ,y)
        elseif tallBlocks [i] == 2 then
            sprite("Planet Cute:Door Tall Closed",x ,y)
        elseif tallBlocks [i] == 3 then
            sprite("Planet Cute:Door Tall Open",x ,y)
        elseif tallBlocks [i] == 4 then
            sprite("Planet Cute:Wall Block Tall",x ,y)
        elseif tallBlocks [i] == 5 then
            sprite("Planet Cute:Window Tall",x ,y)
        else
            
        end
           
        if objectBlocks [i] == 0 then
            
        elseif objectBlocks [i] == 1 then --draws non-interactive objects:
            sprite("Planet Cute:Rock", x, y)
        elseif objectBlocks [i] == 2 then
            sprite("Planet Cute:Tree Short", x, y)   
        elseif objectBlocks [i] == 3 then
            sprite("Planet Cute:Tree Tall", x, y)
        elseif objectBlocks [i] == 4 then
            sprite("Planet Cute:Tree Ugly", x, y)  
        else
            
        end
         
        if interactiveBlocks [i] == 0 then --draws interactive objects
            
        elseif interactiveBlocks [i] == 1 then
            sprite("Planet Cute:Gem Blue", x, y)
        elseif interactiveBlocks [i] == 2 then
            sprite("Planet Cute:Gem Green", x, y)
        elseif interactiveBlocks [i] == 3 then
            sprite("Planet Cute:Gem Orange", x, y)
        elseif interactiveBlocks [i] == 4 then
            sprite("Planet Cute:Key", x, y)
        elseif interactiveBlocks [i] == 5 then
            sprite("Planet Cute:Chest Closed", x, y)
        elseif interactiveBlocks [i] == 6 then
            sprite("Planet Cute:Chest Open", x, y)
        else
          
        end   
        
        if player [i] == 1 then           
            sprite("Planet Cute:Character Boy", x, y)
            spx = x
            spy = y
        else
            
        end            
        y = y - 40                                                                   
        
        x = x + 101
        
        if n > 7 then
            y = y - 83
            x = 50
            n = 0
        end    
        n = n + 1  
    end
    
    if CurrentTouch.state == BEGAN then --tells which button has been pressed
        time = time + 1
        if time > 2 then
            if WIDTH - 75 < CurrentTouch.x then --has the down button been pressed?
                if CurrentTouch.x < WIDTH - 25 then
                    if CurrentTouch.y < 454 then
                        if CurrentTouch.y > 400 then
                            if tallBlocks[spsq + 8] == 0 then --is there anything in the way?
                                if objectBlocks[spsq + 8] == 0 then
                                    player[spsq] = 0 -- move down
                                    spsq = spsq + 8
                                    if spsq > 56 then
                                        spsq = spsq - 56
                                    end
                                    player[spsq] = 1
                                    posChange = 1
                                    time = -2
                                end
                            end
                        end
                    end
                end
            end
                        
            if WIDTH - 75 < CurrentTouch.x then --has the up button been pressed?
                if CurrentTouch.x < WIDTH - 25 then
                    if CurrentTouch.y < 554 then
                        if CurrentTouch.y > 500 then
                            if tallBlocks[spsq - 8] == 0 then --is there anything in the way?
                                if objectBlocks[spsq - 8] == 0 then
                                    player[spsq] = 0 -- move up
                                    spsq = spsq - 8
                                    if spsq < 1 then
                                        spsq = spsq + 56
                                    end
                                    player[spsq] = 1
                                    posChange = 1
                                    time = -2
                                end
                            end
                        end
                    end
                end
            end
                        
            if WIDTH - 75 < CurrentTouch.x then --has the left button been pressed?
                if CurrentTouch.x < WIDTH - 25 then
                    if CurrentTouch.y < 654 then
                        if CurrentTouch.y > 600 then
                            if tallBlocks[spsq - 1] == 0 then --is there anything in the way?
                                if objectBlocks[spsq - 1] == 0 then
                                    player[spsq] = 0 -- move left
                                    spsq = spsq - 1
                                    if spsq < 1 then
                                        spsq = spsq + 56
                                    end
                                    player[spsq] = 1
                                    posChange = 1
                                    time = -2
                                end
                            end
                        end
                    end
                end
            end
                        
            if WIDTH - 75 < CurrentTouch.x then --has the right button been pressed?
                if CurrentTouch.x < WIDTH - 25 then
                    if CurrentTouch.y < 754 then
                        if CurrentTouch.y > 700 then
                            if tallBlocks[spsq + 1] == 0 then --is there something in the way
                                if objectBlocks[spsq + 1] == 0 then
                                    player[spsq] = 0 -- move right
                                    spsq = spsq + 1
                                    if spsq > 56 then
                                        spsq = spsq - 56
                                    end
                                    player[spsq] = 1
                                    posChange = 1
                                    time = -2
                                end
                            end
                        end
                    end
                end
            end        
        end
    end  
end

Quick tip: use ~~~ before and after your code to make it display correctly. And welcome to the forums!

@Coder Check your “if” statements. You have some “else” statements that shouldn’t be there. After I removed the “else” statements, I was able to move the sprite around the screen using the arrow keys. Wasn’t able to pick up the key to unlock the chest. I guess that’s next.

@Coder Now that you fixed your code so it displays better, remove the “else” statements at line 89, 103, 121, and 129.

Thanks it works great now