Can somebody correct my Table-Load-Save code? Please~~
It can’t work. Thank you very much.
My code is below :
local json = require(“json”)
local save
local load
local user = {}
function save(filename, userValue)
local path = system.pathForFile( filename, system.DocumentsDirectory)
local file = io.open(path, “w”)
if file then
local contents = json.encode(userValue)
file:write( contents )
io.close( file )
end
end
function load(filename)
local path = system.pathForFile( filename, system.DocumentsDirectory)
local contents = “”
local userValue = {}
local file = io.open( path, “r” )
if file then
local contents = file:read( “*a” )
userValue = json.decode(contents);
io.close( file )
return userValue
else
userValue.bloodBarLength = 300
userValue.coin = 0
save(userValue, “userValue.json”)
return userValue
end
end
user = load(“user.json”)
print(user.bloodBarLength)
print(user.coin)
save(userValue, “userValue.json”)