user3558131
user3558131

Reputation: 113

Error when trying to add a point to my score counter. Lua

I have an error that I have run into when trying to create my score counter for my lua game. Here is the code that I have.

score = 0

local playerScore = display.newText("Score" ..score, 0, 10, "AmericanTypewriter-Bold",          16);
playerScore:setTextColor(0, 0, 0);


playerScore.text = "Score: " .. score

function ball:touch( event )
if event.phase == "began" then
playerScore.text = playerScore.text + 1 
ball:applyForce(0, -10)
return true
end
end 

Here is the line that gives me the error.

playerScore.text = playerScore.text + 1 

The error that it gives me.

Attempt to perform arithmetic on field 'text' (a string value)

Upvotes: 0

Views: 245

Answers (1)

Jan Berktold
Jan Berktold

Reputation: 951

You are attempting to add 1 to the string "Score: 1" (where 1 may be any number), instead you should increment the score variable and then update the text.

This should do the job.

score = score + 1
playerScore.text = "Score: " .. score

Upvotes: 1

Related Questions