Hello. I am trying to access a json file here which will store the highscores for my game. This bit of code here is so I can access the file and I will make it so the high score is displayed on the title screen.
local json = require("json")
local function loadHighscore(filename)
local content = sys.load(filename)
if content == nil then
print("Failed to load JSON content from file: " .. filename)
return nil
end
local success, data = pcall(json.decode, content)
if not success or not data or not data.numbers then
print("Invalid or missing data in JSON file: " .. filename)
return nil
end
table.sort(data.numbers, function(a, b) return a > b end)
--Return the highest score (first value)
return data.numbers[1]
end
function init(self)
self.file_path = sys.get_save_file("main", "test.json")
local highscore = loadHighscore(self.file_path)
end
I have accessed the json file using the same method in another script to sort the data however when this script runs it crashes. This is what the console reads:
Why is this happening?