the button doesn't work immediately

Hi, I’m a beginner, unfortunately on the internet aren’t many guides about the Lua language in Polish, and I’m not a master of English :slight_smile: I have a question about button operation. I created a code that after pressing the button is to reduce the amount of myScore by 5 points, but when I click the button nothing happens. Only after collecting points, myScore is changing (if I click twice before and collect 1 orb(points) myScore = -9)

.

.

.

local myScore = 0

local tapText = display.newText(myScore, 0, 19, native.systemFont, 25)

–orb collection function (collecting points) :

local function trackOrbs(obj)
    myScore = myScore + 1
    tapText.text = myScore
    obj:removeSelf();
end

local function spawnOrb()
    local orb = display.newImageRect(“moneta.png”, 15, 15)
    orb.x =mRand(50, _W-50); orb.y = 230;
    physics.addBody(orb, “static”, {radius = 1})
    
    function orb:collision(e)
        if (e.phase == “began”) then
        trackOrbs(self);
    
        end
        return true;
    end
    
        orb:addEventListener(“collision”, orb);
                
    end
    tmr = timer.performWithDelay(20, spawnOrb, total_orbs)


**local function btnf(event)
myScore = myScore - 5
end

local btn = widget.newButton {
label=“Buy”,
onRelease = btnf,
fontSize = 20,
width = 190
 }**

I also don’t understand why it shows me an error when, in the button’s parameters, it adds for example height = 30

thank you very much for every answer!!! :slight_smile:

Your button listener only reduces the value of  myScore by 5, but it doesn’t actually do anything to the text display object, i.e.  tapText. If you want to visually update the text, then you must do that separately. See what you’ve done in function  trackOrbs.

Thank you very much, I added “tapText.text = myScore” and works correctly :slight_smile:

You may know the answer to my second question regarding the display error after writing “height = 123” in button parameters? :slight_smile:

Prolly you forgot a comma. No one knows if you dont show error message.

I don’t know what I was doing wrong, the error said that the text is too close to " } " . Now everything is good and I have changed nothing :slight_smile:

eh I know what I did wrong (my second question), I’m ashamed to admit it, but I just didn’t put in " , "

@strusinski  No shame is that!  It always comes down a “,” or a “.” or a “:”  :wink: