[Solved] Help with if/then statement

Hi guys/gals,
Sorry this is so basic, but I’m pulling my hair out with this one.

At the end of my game it checks to see if the score you just got is higher than the highscore and changes the game over message accordingly. This is the code I have

[lua]if playeronescore > highScore then
print(“Set new high score!!”)
highScore = playeronescore
gameoverMessage.text=“A NEW HIGH SCORE!!”
else
gameoverMessage.text=“You scored”
end[/lua]
When it runs (and you’ve set a high score) it prints to the console to say a new high score has been set, but then runs the second part of the statement and sets the gameover message to say “You scored”.

What am I doing wrong on this???
[import]uid: 7841 topic_id: 23576 reply_id: 323576[/import]

It is executing the code twice. No doubt. Look for the outer loop that is not stopping or the “state” that is not getting set. Been there! lol! [import]uid: 21331 topic_id: 23576 reply_id: 94547[/import]

Try something like this:

[code]
local oldHighScore = highScore

if playeronescore > oldHighScore then
print(“Set new high score!!”)
highScore = playeronescore
gameoverMessage.text=“A NEW HIGH SCORE!!”
else
gameoverMessage.text=“You scored”
end
[/code] [import]uid: 12979 topic_id: 23576 reply_id: 96136[/import]

Thanks it helped :slight_smile: [import]uid: 48300 topic_id: 23576 reply_id: 100777[/import]

@TheRealTonyK
Sorry it’s taken me so long to answer. Of course, you were indeed right, it was running twice, but not as part of a loop.
The first time the game ran it would all work fine, but I was failing to remove an event listener at the game over stage, so the next time the game ran it would add another listener to the object and would trigger twice, then three times etc.
Took me blinkin ages to figure it out :slight_smile: [import]uid: 7841 topic_id: 23576 reply_id: 101289[/import]