Help Creating a Score Counter and saving Highscore

Heyo!

I need help creating a Score counter that counts by the second(Game1.lua). And when a collision happens it stops, and I already have it so onCollision you go to GameOver.lua. Now on GameOver.lua I want it to display the score the timer reached before the collision, and your current Highscore. And to change the Highscore everytime you beat it.

I can give you my Menu, Game, or Gameover.lua Code if needed.

I don’t want you guys to give me LINKS to other things. I checked them all out already and I really need help from a real person.

I just need help from you guys, maybe step-by-step if needed.

NO links. They wont really help.

I am only posting this and saying “No Links” because I have check them all and none helped. And I really need to do this now. I have been trying to do this for quite some time now! And need to just finish it!

Please and thank you.

local scoreTimer local score = 0 local updateScore = function() score = score + 1 end scoreTimer = timer.performWithDelay(1000,updateScore,-1) --when you detect collision timer.cancel(scoreTimer)

Thanks for the reply. I tried it out but It’s just the same, no text or anything.

I do have this:

local onCollision = function(self, event) if event.phase == "began" then local hit = self.value local other = event.other.value local removeScene if other == 1 then display.remove(Player) --display.remove(box[i]) audio.play( soundTable["Dead"] ) timer.cancel( boxTimer ) timer.cancel( box2Timer ) timer.cancel( box3Timer ) --timer.cancel( box4Timer ) --Runtime:addEventListener("enterFrame", cancelMaybeTimer ) display.remove(scoreTxt) Runtime:addEventListener("enterFrame", resetScore) Runtime:addEventListener("enterFrame", cancelTimer) Runtime:removeEventListener( "accelerometer", onAccelerate ) --timer.cancel( scoreText ) --Player:removeEventListener("touch", scoreText) --composer.removeScene("Game1") composer.gotoScene( "GameOver", {time = 500, effect = "zoomOutIn"}) --Runtime:addEventListener( "collision", changeScene ) composer.removeScene("Game1") end return true end end 

and I put

timer.cancel(scoreTimer)

into the onCollision code, but everything is the same. No Score or anything…

I don’t see where you are creating your score text object, you are doing this?

local options = { text = "", x = 100, y = 200, width = 128, font = native.systemFontBold, fontSize = 18, align = "right" } local myText = display.newText( options ) myText:setFillColor( 1, 0, 0 ) -- and more importantly -- myText.text = score

Alright, so here is the entire “Game1.lua” code:

EDIT: I removed the code. If needed, just ask.

I bolded and underlined the one’s you gave me, so you know where I put them.

I am pretty knew at this, so my apologies for the messy code.

Otherwise, here is the ERROR I am recieving: 

_ addEventListener: listener cannot be nil: nil stack traceback: _

[C]: in function 'error’

_ ?: in function ‘getOrCreateTable’ _

_ ?: in function ‘addEventListener’ _

_ ?: in function ‘addEventListener’ _

_ Game1.lua:178: in function <Game1.lua:161> _

_ ?: in function <?:221> _

So I have no idea what’s happening :confused:

Ok then. I just saw it did not underline or bold the code…

Here is where the things are:

two of the codes you gave me are under:

 local box = {} local i = 1 local boxTimer local box2 = {} local i = 1 local box2Timer local box3 = {} local i = 1 local box3Timer local box4 = {} local i = 1 local box4Timer local delayTimer local box5 = {} local i = 1 local box5Timer 

and the other one is inside the “onCollision” Code

I don’t want you to look through the ENTIRE thing, just where I put your code, sorry for posting the entire code though.

Sorry, you can’t just throw my code into your code and expect it to work, that’s not what it was meant for.

I just copied and pasted the text example from Corona Docs.

My code snippet was just an example of how to update the score every second, it needs to be modified to fit into your code to function properly.

For starters you will need to put myText.text = score inside the updateScore function to see the score visibly increase while the game is running.

You will also need to save the score to a json file and load it in your game over module, see Rob Miracles loadsave.lua for this it does all the hard work for you.

The error you are getting on line 178? Where is line 178 in all that code? When you insert the code you have the option to turn on line numbering, but even then all the comments “–” are confusing things.

I’m very busy with my own project so I can’t go through your code and fix everything, maybe someone else has the time, good luck I hope you get it all working, sorry I can’t help you more, I have my own problems.

Alright, Thanks for your time! Although I already Do have a modified version that works with my code, the only problem with that is that when it resets after you restart the game, it just gets stuck at 1 instead of going up.

Just know that I don’t want you to do all of my work for me, I just need help, I don’t want things to just be done.

Here is the Code for the Modified version.

You don’t need to do anything, just throwing it out there!
 

score = 0 local scoreTxt = display.newText( "0", 0, 0, "Helvetica", 40 ) scoreTxt.x = display.contentCenterX scoreTxt.y = 150 local function updateScore(event) score = score + 1 -- just a timer so you can keep track.. scoreTxt.text = string.format("%d", score ) end scoreTimer = timer.performWithDelay(1000, updateScore, 0) -- AFTER SOME CODE AND ETC: local function resetScore(event) score = 0 scoreTxt.text = string.format("Score: %d", score ) end -- PUT THIS ONE IN YOUR "onCollision" TYPE OF CODE: display.remove(scoreTxt) Runtime:addEventListener("enterFrame", resetScore) Runtime:addEventListener("enterFrame", cancelTimer)

I also need help on this. It’s a pretty big issue for a lot of users. So if anyone knows how…

And also, thanks Spid3rtech! Your last post helped!

I can see why your score is getting stuck at 1

Runtime:addEventListener(“enterFrame”, resetScore)
Runtime:addEventListener(“enterFrame”, cancelTimer)

These will fire 30 to 60 times every second!

And if you don’t cancel them they will keep on running even if you are in a different module.

Sweet! Thank you!

But now, do you think you can help me get started on how to save the score to the GameOver screen? Because this is one of the things I am having trouble with… which is why I started this forum.

Open notepad on your computer and create a simple text file.

In that text file type 0

That’s a zero by the way, and save it to your game folder where your Game1.lua is and call it highScore.txt

If you want to start the highScore with a higher number then put that instead of zero when you create it.

In your main.lua put this code in.

local highScore \_path = system.pathForFile( "highScore.txt", system.ResourceDirectory ) \_file = io.open( \_path, "r" ) for line in \_file:lines() do highScore = tonumber (line) end io.close( \_file ) \_file = nil \_path = nil \_path = system.pathForFile( "highScore.txt", system.DocumentsDirectory ) \_file = io.open( \_path, "w" ) \_file:write( highScore ) io.close( \_file ) \_file = nil \_path = nil

It reads the highScore.txt you just put in your game folder and gives the local variable highScore the value you typed in before.

It then writes the highScore.txt to the system.Documents directory where it needs to be when you build the game for the device, don’t worry about this part.

At the top of every module in your game only where you use the score put this:

local highScore \_path = system.pathForFile( "highScore.txt", system.DocumentsDirectory ) \_file = io.open( \_path, "r" ) for line in \_file:lines() do highScore = tonumber (line) end io.close( \_file ) \_file = nil \_path = nil

The above loads the highScore.

Keep using the score the way you are now.

In your Game1.lua when the game is over check the score to see if it’s higher than the highScore

if score \> highScore then highScore = score \_path = system.pathForFile( "highScore.txt", system.DocumentsDirectory ) \_file = io.open( \_path, "w" ) \_file:write( highScore ) io.close( \_file ) \_file = nil \_path = nil end

The above writes the new highScore to the highScore.txt document.

“w” means write to and “r” means read from.

That’s the basics, you can easily see how it works and you can add to it all you like, have fun.

I realize that you said you don’t want links, but what QuizMaster is describing, is exactly what is included in the tutorial below. This tutorial is specifically for displaying AND saving scores, which is exactly what you want:

https://coronalabs.com/blog/2013/12/10/tutorial-howtosavescores/

Your best bet would be to use this tutorial. If you encounter problems with the tutorial, then you can post here, asking questions about points inside the tutorial would be the best way to make sure everyone is on the same page.

@Alex@Panc, well see… your link is good but for someone like me its incredible confusing… @QuizMaster made this very easy to understand. What im thinking is im going to make a YouTube video to help out the ones making their first games and need a simple score…

@Alex

I remember when I first started programming and those tutorials might be good but to a newbie they are incredibly complicated to understand. That’s why he doesn’t want links, he’s probably looked at them already, can’t figure it out and that’s why he’s asking people like us for help.

And when you do give help keep it simple and break everything down into easy steps, clearly saying where they have to put everything and what the parts do.

Lather, rinse, repeat…

My intent in posting that tutorial was to get a more focused conversation started from one sheet of music. We all want to help, and the available tutorials are supposed to be helping new developers who require assistance. If you’re saying they aren’t useful, then learning about how they can be made more useful would benefit everyone. If you guys are saying those tutorials are too complicated, it would be good to know how they can be made more simplistic, so they can be improved.

As it is, this tutorial in particular, is one of the more simple ones that have been created. It is using, basically, the same exact methodology that @QuizMaster has used above. The good thing about the tutorial, is that it also teaches new developers about creating modules, and introduces the concept of saving data for later use. These are always valuable skills to have, which is why tutorials are made in the first place.

No offence meant, as I was just trying to provide help to OP.

Well yeah, I get that you were trying to provide help. But the thing is, I don’t know a a whole lot of lua coding, and I am pretty busy some times. So basically I was trying to get the simplest form of that, which Quizmaster has provided. But right now, before I can even see if that works, I have this other small issue that I am figuring out. So when that’s done I will come back to check out all of his code.

If you are so new to Lua that reading a tutorial that pretty simply explains the problem you’re trying to solve, then perhaps you need to drop back and spend some times learning the basics.  This is how learning works.  You can’t be successful in your Organic Chemistry class until you’ve gone through Basic Chemistry. You can’t be successful in Basic Chemistry until you’ve learned the high school basic science classes.  The score tutorial above involves saving files, using functions, display groups, etc. It’s not going to make sense until you under stand variables, flow control (if’s loops, etc.), functions and tables.  People might be able to learn to swim by being thrown int he deep end of the pool, but for programming it’s best to start in the shallow end and work your way up.

Rob