Attempting to code cannon

It’s not working, I Think because since it’s the first game the code doesn’t know what the hiscore is yet.

function setup()
 supportedOrientations(LANDSCAPE_LEFT)
 displayMode(FULLSCREEN)
    rTab={}     --table for multiple rocks
    cTab={}     --table for multiple cubes
    sTab={}    
    x,y=WIDTH*1/8,HEIGHT/2  -- position of ship
    a,b=-100,WIDTH/2
    ang=0       -- starting angle for ship
    mTab={}     -- table for multiple missiles
    mSpeed=10  -- speed of the missile
    tx,ty=0,0
    delay=10    -- used with limit
    limit=10    -- delay between missile shots
    maxRocks=3 -- max number of rocks to show
    maxCubes=1  -- max number of cubes
    maxSoldiers=1
    createRock()
    createCube()
    createSoldier()    
    Hearts=5
    Score=0
    hiscore=readLocalData("hiscore")

end
    
function createRock()   -- create up to maxRocks on the screen
    while #rTab<maxRocks do
        table.insert(rTab,vec3(WIDTH+20,math.random(760),math.random(-5,-1)))
    end
end

function createCube()   -- create up to maxCubes on the screen
    while #cTab<maxCubes do
        table.insert(cTab,vec3(WIDTH+20,math.random(760),math.random(-6,-4)))
    end
end

function createSoldier()
    while #sTab<maxSoldiers do
        table.insert(sTab,vec3(WIDTH+20,math.random(760),math.random(-10,-1)))
    end
end

function touched(touch)
    if touch.state==BEGAN or touch.state==MOVING then  
        if not gameOver then     
            ang=math.deg(math.atan(touch.y-y,touch.x-x))
            shoot=true
            tx=touch.x
            ty=touch.y
        end
    end 
    if touch.state==ENDED then
        shoot=false
    end

end

function draw()
    background()
    --if not gameOver then
        fill(255, 0, 0, 255)
    
        sprite()
        sprite("Documents:battle ground", WIDTH/2, HEIGHT/2, 1700, 900)
        pushMatrix()    
        fontSize(20)    
        text("Bullet Speed;", WIDTH*26/100, HEIGHT*9/10) 
        text(mSpeed, WIDTH/3, 9/10*HEIGHT)
        text("Bullet Delay;", WIDTH*68/100, HEIGHT*9/10)
        text(limit, WIDTH*3/4, 9/10*HEIGHT)  
        text("Lives;",WIDTH*18/40, HEIGHT*29/30)
        text(Hearts,WIDTH*39/80, HEIGHT*29/30)
        text("Score;", WIDTH*1/20,HEIGHT*29/30)
        text(Score, WIDTH*2/20, HEIGHT*29/30)
        translate(x,y)
        rotate(ang-270)
        sprite("Tyrian Remastered:Boss B", 0,0,90,90)
        translate()
        popMatrix()
    --end

    -- loop thru missile table
    for a,b in pairs(mTab) do
        sprite("Tyrian Remastered:Explosion Ball",b.x,b.y)       
        b.x=b.x+b.z*mSpeed
        b.y=b.y+b.w*mSpeed
        if b.x>WIDTH or b.x<0 and b.y>HEIGHT or b.y<0 then
            table.remove(mTab,a)
        end

        -- check if missile and rocks are less than 30 pixels apart
        v1=vec2(b.x,b.y)    -- missle x,y values
        for a1,b1 in pairs(rTab) do -- loop thru rock table
            d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and missile
            if d<30 then
                table.remove(rTab,a1)   -- remove rock from the table
                sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
                sound(SOUND_EXPLODE, 39131)
                createRock()    -- create rocks
                Score=Score+5
              end
        end

        for a2, b2 in pairs(sTab) do
            d=v1:dist(vec2(b2.x,b2.y))
            if d<5 then
                table.remove(sTab, a2)
                sprite("Tyrian Remastered:Space Ice 3",b2.x,b2.y)
                sound(SOUND_EXPLODE, 39131)
                createSoldier()
                limit=limit-0.1
            end
        end

         -- check if missile and cubes are less than 10 pixels apart
        for a1,b1 in pairs(cTab) do -- loop thru rock table
            d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and missile
            if d<5 then
                table.remove(cTab,a1)   -- remove cube from the table
                sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
                sound(SOUND_EXPLODE, 39131)
                createCube()    -- create cubes
                mSpeed=mSpeed+1
            end
        end
    end

    -- check if a rock hits the ship
    v1=vec2(x,y)    -- position of ship
    for a1,b1 in pairs(rTab) do -- loop thru rock table
        d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and ship
        if d<50 then
            table.remove(rTab,a1)
            sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
            sound(SOUND_EXPLODE, 39131)
            gameOver=true
            shoot=false -- dont allow any shooting
            x,y=999,999 -- move ship off screen
        end
    end

    delay=delay+1
    -- delay shooting
    if shoot and delay>limit then
        v1=vec2(tx-x,ty-y)
        v1=v1:normalize()           
        table.insert(mTab,vec4(x,y,v1.x,v1.y))
        delay=0
        sound(SOUND_SHOOT, 15498)
    end

    -- loop thru rock table and draw them
    for a,b in pairs(rTab) do
        sprite("Tyrian Remastered:Fire Rock",b.x,b.y, 50, 50)
        b.x=b.x+b.z -- decrement rock position
        if b.x<-20 then     -- rocks are off the left side of the screen
            table.remove(rTab,a)    -- remove rock from table
            createRock()    -- create another random rock
            Hearts=Hearts-1
           end  
    end

    -- loop thru cube table and draw them
    for c,d in pairs(cTab) do
        sprite("Tyrian Remastered:Powerup 19",d.x,d.y)
        d.x=d.x+d.z -- decrement cube position
        if d.x<-5 then     -- cubes are off the left side of the screen
            table.remove(cTab,c)    -- remove cube from table
            createCube()   
        end
    end

    for e,f in pairs(sTab) do
        sprite("Tyrian Remastered:Powerup 18",f.x,f.y)
        f.x=f.x+f.z
        if f.x<-5then
            table.remove(sTab,e)
            createSoldier()
        end
    end

        if Hearts == 0 then
      
          gameOver=true
    end
        
       if Score>hiscore, and gameOver==true, then

        Score=saveLocalData("hiscore")
     end
        if gameOver == true then
        background(212, 35, 35, 255)
        font("ArialRoundedMTBold")
        text("GAME OVER", WIDTH/2, HEIGHT/2)
        fontSize(50) 
        text(Score, WIDTH/2, HEIGHT*18/40)
        text(hiscore, WIDTH/2, HEIGHT*22/40)
        
          end
end

When you get highscore in setup, if it’s nil, set it to 0. The first time you read it, it’s going to be nil.

I did it but it still says it is nil

mTab={}     -- table for multiple missiles
    mSpeed=10  -- speed of the missile
    tx,ty=0,0
    delay=10    -- used with limit
    limit=10    -- delay between missile shots
    maxRocks=3 -- max number of rocks to show
    maxCubes=1  -- max number of cubes
    maxSoldiers=1
    createRock()
    createCube()
    createSoldier()    
    Hearts=5
    Score=0
    hiscore=readLocalData("hiscore")

    if readLocalData("hiscore")~=nil then
        hiscore=readLocalData("hiscore")
        hiscore=math.floor(hiscore)
    else
        hiscore=0
    
    
end
end
    

    function createRock()   -- create up to maxRocks on the screen
    while #rTab<maxRocks do
        table.insert(rTab,vec3(WIDTH+20,math.random(760),math.random(-5,-1)))
    end
end

function createCube()   -- create up to maxCubes on the screen
    while #cTab<maxCubes do
        table.insert(cTab,vec3(WIDTH+20,math.random(760),math.random(-6,-4)))
    end
end

function createSoldier()
    while #sTab<maxSoldiers do
        table.insert(sTab,vec3(WIDTH+20,math.random(760),math.random(-10,-1)))
    end
end

function touched(touch)
    if touch.state==BEGAN or touch.state==MOVING then  
        if not gameOver then     
            ang=math.deg(math.atan(touch.y-y,touch.x-x))
            shoot=true
            tx=touch.x
            ty=touch.y
        end
    end 
    if touch.state==ENDED then
        shoot=false
    end

end

function draw()
    background()
    --if not gameOver then
        fill(255, 0, 0, 255)
    
        sprite()
        sprite("Documents:battle ground", WIDTH/2, HEIGHT/2, 1700, 900)
        pushMatrix()    
        fontSize(20)    
        text("Bullet Speed;", WIDTH*26/100, HEIGHT*9/10) 
        text(mSpeed, WIDTH/3, 9/10*HEIGHT)
        text("Bullet Delay;", WIDTH*68/100, HEIGHT*9/10)
        text(limit, WIDTH*3/4, 9/10*HEIGHT)  
        text("Lives;",WIDTH*18/40, HEIGHT*29/30)
        text(Hearts,WIDTH*39/80, HEIGHT*29/30)
        text("Score;", WIDTH*1/20,HEIGHT*29/30)
        text(Score, WIDTH*2/20, HEIGHT*29/30)
        text(hiscore, WIDTH*19/20, HEIGHT*29/30)
        translate(x,y)
        rotate(ang-270)
        sprite("Tyrian Remastered:Boss B", 0,0,90,90)
        translate()
        popMatrix()
   
     --end

    -- loop thru missile table
    for a,b in pairs(mTab) do
        sprite("Tyrian Remastered:Explosion Ball",b.x,b.y)       
        b.x=b.x+b.z*mSpeed
        b.y=b.y+b.w*mSpeed
        if b.x>WIDTH or b.x<0 and b.y>HEIGHT or b.y<0 then
            table.remove(mTab,a)
        end

        -- check if missile and rocks are less than 30 pixels apart
        v1=vec2(b.x,b.y)    -- missle x,y values
        for a1,b1 in pairs(rTab) do -- loop thru rock table
            d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and missile
            if d<30 then
                table.remove(rTab,a1)   -- remove rock from the table
                sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
                sound(SOUND_EXPLODE, 39131)
                createRock()    -- create rocks
                Score=Score+5
              end
        end

        for a2, b2 in pairs(sTab) do
            d=v1:dist(vec2(b2.x,b2.y))
            if d<5 then
                table.remove(sTab, a2)
                sprite("Tyrian Remastered:Space Ice 3",b2.x,b2.y)
                sound(SOUND_EXPLODE, 39131)
                createSoldier()
                limit=limit-0.1
            end
        end

         -- check if missile and cubes are less than 10 pixels apart
        for a1,b1 in pairs(cTab) do -- loop thru rock table
            d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and missile
            if d<5 then
                table.remove(cTab,a1)   -- remove cube from the table
                sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
                sound(SOUND_EXPLODE, 39131)
                createCube()    -- create cubes
                mSpeed=mSpeed+1
            end
        end
    end

    -- check if a rock hits the ship
    v1=vec2(x,y)    -- position of ship
    for a1,b1 in pairs(rTab) do -- loop thru rock table
        d=v1:dist(vec2(b1.x,b1.y))  -- distance between rock and ship
        if d<50 then
            table.remove(rTab,a1)
            sprite("Tyrian Remastered:Explosion Huge",b1.x,b1.y)
            sound(SOUND_EXPLODE, 39131)
            gameOver=true
            shoot=false -- dont allow any shooting
            x,y=999,999 -- move ship off screen
        end
    end

    delay=delay+1
    -- delay shooting
    if shoot and delay>limit then
        v1=vec2(tx-x,ty-y)
        v1=v1:normalize()           
        table.insert(mTab,vec4(x,y,v1.x,v1.y))
        delay=0
        sound(SOUND_SHOOT, 15498)
    end

    -- loop thru rock table and draw them
    for a,b in pairs(rTab) do
        sprite("Tyrian Remastered:Fire Rock",b.x,b.y, 50, 50)
        b.x=b.x+b.z -- decrement rock position
        if b.x<-20 then     -- rocks are off the left side of the screen
            table.remove(rTab,a)    -- remove rock from table
            createRock()    -- create another random rock
            Hearts=Hearts-1
           end  
    end

    -- loop thru cube table and draw them
    for c,d in pairs(cTab) do
        sprite("Tyrian Remastered:Powerup 19",d.x,d.y)
        d.x=d.x+d.z -- decrement cube position
     
               if d.x<-5 then     -- cubes are off the left side of the screen
            table.remove(cTab,c)    -- remove cube from table
            createCube()   
        end
    end

    for e,f in pairs(sTab) do
        sprite("Tyrian Remastered:Powerup 18",f.x,f.y)
        f.x=f.x+f.z
        if f.x<-5then
            table.remove(sTab,e)
            createSoldier()
        end
    end

        if Hearts == 0 then
      
          gameOver=true
    end
        
     
       
     end
        if gameOver == true then
        background(212, 35, 35, 255)
        font("ArialRoundedMTBold")
        text("GAME OVER", WIDTH/2, HEIGHT/2)
        fontSize(50) 
        text(Score, WIDTH/2, HEIGHT*18/40)
        text(hiscore, WIDTH/2, HEIGHT*22/40)
         end
          if Score>hiscore then

        Score=saveLocalData("hiscore")
          end

@NG_2000 Add this in setup hiscore=readLocalData("hiscore",0). It will set hiscore to 0 if hiscore isn’t there. Add this saveLocalData(“hiscore”,hiscore) to save your high score at gameover. Try looking at the built in reference to see the format of the commands when something doesn’t work.