Attempt to index upvalue score (a number value)

I copied and pasted my game into a new project to simulate someone who just downloaded the game, but I am getting an error with my score.get() method: 

 function highScore() currentScore = points if currentScore \> score.get() then (error line) score.set(currentScore) end score.save() end

I look in the project sandbox and realize there is no scorefile.txt, so there is nothing to read the scores from.

My question is how can you create a default scorefile.txt with a score of 0, as soon as the game starts for the first time? If you need any more code or other files, feel free to ask.

I am using score.lua to save my scores:

local M = {} M.score = 0 function M.init(options) local customOptions = options or {} local opt = {} opt.fontSize = customOptions.fontSize or 24 opt.font = customOptions.font or native.systemFontBold opt.x = customOptions.x or display.contentCenterX opt.y = customOptions.y or opt.fontSize\*0.5 opt.maxDigits = customOptions.maxDigits or 6 opt.leadingZeros = customOptions.leadingZeros or false M.filename = customOptions.filename or "scorefile.txt" local prefix = "" if (opt.leadingZeros) then prefix = "0" end M.format = "%" .. prefix .. opt.maxDigits .. "d" M.scoreText = display.newText(string.format(M.format, 0), opt.x, opt.y, opt.font, opt.fontSize) return M.scoreText end function M.set(value) M.score = value M.scoreText.text = string.format(M.format, M.score) end function M.get() return M.score end function M.add(amount) M.score = M.score + amount M.scoreText.text = string.format(M.format, M.score) end function M.save() local path = system.pathForFile(M.filename, system.DocumentsDirectory) local file = io.open(path, "w") if (file) then local contents = tostring(M.score) file:write(contents) io.close(file) return true else print("Error: could not read ", M.filename, ".") return false end end function M.load() local path = system.pathForFile(M.filename, system.DocumentsDirectory) local contents = "" local file = io.open(path, "r") if (file) then local contents = file:read("\*a") local score = tonumber(contents); io.close(file) return score else print("Error:could not read scores from ", M.filename, ".") end return nil end return M

Thank you.

If possible please recommend tutorials, or an explanation of the documentation on the Corona API’s list would also be really helpful.

Can you take a look at this -> https://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Can you post the whole error message preferably copy and pasted from the console log window?

Rob

Of course, this is the error:

 ERROR: Runtime error /Users/Alex/Desktop/Margery Jumps/level1.lua:227: attempt to index upvalue 'score' (a number value) stack traceback: /Users/Alex/Desktop/Margery Jumps/level1.lua:227: in function \</Users/Alex/Desktop/Margery Jumps/level1.lua:225\> ?: in function 'dispatchEvent' ?: in function '\_nextTransition' ?: in function \<?:1492\> ?: in function 'gotoScene' /Users/Alex/Desktop/Margery Jumps/levelselect.lua:42: in function '\_onEvent' ?: in function '?' ?: in function \<?:449\> ?: in function \<?:169\>

@bgmadclown, I had come across that documentation before, but I could not figure out a way to implement that into the code, without ruining anything. I believe changes would have to be made in score.lua in the M.load() function:

function M.load() local path = system.pathForFile(M.filename, system.DocumentsDirectory) local contents = "" local file = io.open(path, "r") if (file) then local contents = file:read("\*a") local score = tonumber(contents); io.close(file) return score else print("Error:could not read scores from ", M.filename, ".") end return nil end
file = io.open( path, "w" ) file:write( "0" ) io.close(file)

You’ll need to add something like this to the else case. Can you try it out?

Thank you so much, your code was the key, I just made a small alteration, but thank you very, very much!

function M.load() local path = system.pathForFile(M.filename, system.DocumentsDirectory) local contents = "" local file = io.open(path, "r") if (file) then local contents = file:read("\*a") local score = tonumber(contents); io.close(file) return score else file = io.open( path, "w" ) score = 0 file:write(score) io.close(file) end return nil end

I’m glad it’s working for you :slight_smile: Good luck with your project.

My dad says thank you very much for your time and your patience with me, he is very grateful to you and all the other contributors who have been helping me out. BTW, I had no school today, that’s why I am working on it now.

This is a great teaching moment. The error:

attempt to index upvalue ‘score’ (a number value)

“Attempting to index” means it’s trying to look something up in a table.  Modules return tables. Tables have members (functions, variables) that are indexed in the parent table.  This tells me you’re trying to look up a method named “get” in a table named “score”, but Lua found that score is a number, not table.

This is likely due to something like:

local score = require("score")

then later on doing:

score = 0

You basically can’t use the same variable for two different purposes.  “score” is a logical name to hold the value of the score. It’s perhaps not the best name for the module. Maybe you should consider:

local scoreManager = require(“score”) – though I would rename score.lua to scores.lua or scoremanager.lua…

Then call scoreManager.get() just so it’s obviously different that “score”. 

Rob

If possible please recommend tutorials, or an explanation of the documentation on the Corona API’s list would also be really helpful.

Can you take a look at this -> https://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Can you post the whole error message preferably copy and pasted from the console log window?

Rob

Of course, this is the error:

 ERROR: Runtime error /Users/Alex/Desktop/Margery Jumps/level1.lua:227: attempt to index upvalue 'score' (a number value) stack traceback: /Users/Alex/Desktop/Margery Jumps/level1.lua:227: in function \</Users/Alex/Desktop/Margery Jumps/level1.lua:225\> ?: in function 'dispatchEvent' ?: in function '\_nextTransition' ?: in function \<?:1492\> ?: in function 'gotoScene' /Users/Alex/Desktop/Margery Jumps/levelselect.lua:42: in function '\_onEvent' ?: in function '?' ?: in function \<?:449\> ?: in function \<?:169\>

@bgmadclown, I had come across that documentation before, but I could not figure out a way to implement that into the code, without ruining anything. I believe changes would have to be made in score.lua in the M.load() function:

function M.load() local path = system.pathForFile(M.filename, system.DocumentsDirectory) local contents = "" local file = io.open(path, "r") if (file) then local contents = file:read("\*a") local score = tonumber(contents); io.close(file) return score else print("Error:could not read scores from ", M.filename, ".") end return nil end
file = io.open( path, "w" ) file:write( "0" ) io.close(file)

You’ll need to add something like this to the else case. Can you try it out?

Thank you so much, your code was the key, I just made a small alteration, but thank you very, very much!

function M.load() local path = system.pathForFile(M.filename, system.DocumentsDirectory) local contents = "" local file = io.open(path, "r") if (file) then local contents = file:read("\*a") local score = tonumber(contents); io.close(file) return score else file = io.open( path, "w" ) score = 0 file:write(score) io.close(file) end return nil end

I’m glad it’s working for you :slight_smile: Good luck with your project.

My dad says thank you very much for your time and your patience with me, he is very grateful to you and all the other contributors who have been helping me out. BTW, I had no school today, that’s why I am working on it now.