funkyvisions’ way is perfectly acceptable, but if the user is allowed to replay a level then you need to account for that, otherwise the score added repeatedly for that single level.
Another way would be to have a table, and keep the scores in that. This way you can also only overwrite a level’s score if they get a higher score.
At the start of the game, set the score for each level to zero:
levelScores = {} for i = 1, numberOfLevels do levelScores[i] = 0 end
Then at the end of a level, see if the score can be stored:
if thisLevelScore \> levelScores[levelNumber] then levelScores[levelNumber] = thisLevelScore end
Then at the end of the game, add all of the scores up:
local endScore = 0 for i = 1, #levelScores do endScore = endScore + levelScores[i] end
As I say, nothing wrong with funkyvisions’ way of doing it, but this is another way to do it if you need to repeat levels (or store scores for individual levels).