Line: 478
Attempt to compare nil with number
I GET THIS EFFIN error when the player dies/triggers a collision event when the score is below the current highscore. WHAT THE HECK IS wrong with it?!
Line: 478
Attempt to compare nil with number
I GET THIS EFFIN error when the player dies/triggers a collision event when the score is below the current highscore. WHAT THE HECK IS wrong with it?!
Have you initialized both the score and highscore variable?
local score = 0 local highscore = 0
I have for score can I just write
if score > local highscore?
No, if highscore does not have a value – a number in your case – it will be considered nil.
And, you need to declare the local highscore variable on its own.
Cheers.
Is 0 considered nil?
Hi Max,
No it’s not. Are you setting both highscore and score to 0, or other numerical value, somewhere at the top of your lua file, or in a globals module?
Cheers.
Not highscore only score
Hi Max,
When you’re using comparison operators like > <, etc the value can’t be nil. So you need to set those variables in advance with a starting 0, or any number that is required. Is there any particular reason why it’s not possible to set the highscore ahead of time in your code?
Cheers.
it works now but when I relaunch my app it resets the highscore back to 0
Hi Max,
I’m guessing you want to save the highscore and then load it on subsequent launches. And then re-saving it if the current sessions score is better than the loaded highscore?
In any case you might look at http://forums.coronalabs.com/topic/43552-here-is-some-code-easy-saving-of-score-high-score-resetting-highscore/?hl=%2Bhow+%2Bto+%2Bsave+%2Bhighscore
Cheers.
I just used my original code and delayed my file being opened by 2 seconds
Have you initialized both the score and highscore variable?
local score = 0 local highscore = 0
I have for score can I just write
if score > local highscore?
No, if highscore does not have a value – a number in your case – it will be considered nil.
And, you need to declare the local highscore variable on its own.
Cheers.
Is 0 considered nil?
Hi Max,
No it’s not. Are you setting both highscore and score to 0, or other numerical value, somewhere at the top of your lua file, or in a globals module?
Cheers.
Not highscore only score
Hi Max,
When you’re using comparison operators like > <, etc the value can’t be nil. So you need to set those variables in advance with a starting 0, or any number that is required. Is there any particular reason why it’s not possible to set the highscore ahead of time in your code?
Cheers.
it works now but when I relaunch my app it resets the highscore back to 0
Hi Max,
I’m guessing you want to save the highscore and then load it on subsequent launches. And then re-saving it if the current sessions score is better than the loaded highscore?
In any case you might look at http://forums.coronalabs.com/topic/43552-here-is-some-code-easy-saving-of-score-high-score-resetting-highscore/?hl=%2Bhow+%2Bto+%2Bsave+%2Bhighscore
Cheers.