Help saving a NWInt to the server!

How do I save money to my server? I have tried all morning, this is what I got.

[lua] function writemoney()
writeme = glon.encode(Money)
file.Write(“gangwars/”…ply:SteamID()…".txt", writeme)
end

hook.Add(“PlayerDeath”, “writemoney”, writemoney)

function readmoney()
readme = file.Read(“gangwars/”…ply:SteamID()…".txt")
money = glon.decode(readme)
end[/lua]

[lua]function EasyLog(s, …)
local ns = string.format(s, …)…"
"
Msg(ns)
ServerLog(ns)
if string.match(s, “error”) then ErrorNoHalt(ns) end
end

require"glon"
function _R.Player:Save()
local worked, result = pcall(glon.encode, self.Data)
if worked then
file.Write(“PlayerData/”…self:UniqueID()…".txt", result)
self:ChatPrint(“Your account has been saved!”)
EasyLog("%q’s (%s) account has been saved!", ply:Name(), ply:UniqueID())
else
self:ChatPrint(“There was an error saving your account!”)
EasyLog(“An error occured while saving %q’s (%s) account: %q!”, ply:Name(), ply:UniqueID(), result)
end
end
function _R.Player:Load()
local worked, result = pcall(glon.decode, file.Read(“PlayerData/”…self:UniqueID()…".txt") or “”)
if worked then
self.Data = result
self:ChatPrint(“Your account has been loaded!”)
EasyLog("%q’s (%s) account has been loaded!", ply:Name(), ply:UniqueID())
else
self:ChatPrint(“There was an error loading your account!”)
EasyLog(“An error occured while loading %q’s (%s) account: %q!”, ply:Name(), ply:UniqueID(), result)
end
end
[/lua]

You’d simply save their money in ply.Data.Money, then call the save function.
To load, you just call the load function and use ply.Data.Money.

Enjoy.

Edit: In fact, you don’t need to use GLON at all if you are just saving their money. It’s not until you want to save more complex information, like inventories and such, that you will need GLON. If you plan to do this in the future, I recommend you stick with GLON for now as it will remove the trouble of converting it later.

[lua]function EasyLog(s, …)
local ns = string.format(s, …)…"
"
Msg(ns)
ServerLog(ns)
if string.match(s, “error”) then ErrorNoHalt(ns) end
end

require"glon"
function _R.Player:Save()
local worked, result = pcall(glon.encode, self.Data)
if worked then
file.Write(“PlayerData/”…self:UniqueID()…".txt", result)
self:ChatPrint(“Your account has been saved!”)
EasyLog("%q’s (%s) account has been saved!", self:Name(), self:UniqueID())
else
self:ChatPrint(“There was an error saving your account!”)
EasyLog(“An error occured while saving %q’s (%s) account: %q!”, self:Name(), self:UniqueID(), result)
end
end
function _R.Player:Load()
local worked, result = pcall(glon.decode, file.Read(“PlayerData/”…self:UniqueID()…".txt") or file.Read(“PlayerData/default.txt”) or “”)
if worked then
self.Data = result
self:ChatPrint(“Your account has been loaded!”)
EasyLog("%q’s (%s) account has been loaded!",self:Name(), self:UniqueID())
else
self:ChatPrint(“There was an error loading your account!”)
EasyLog(“An error occured while loading %q’s (%s) account: %q!”, self:Name(), self:UniqueID(), result)
end
end
function SetDefault(data)
local worked, result = pcall(glon.encode, data)
if worked then
file.Write(“PlayerData/default.txt”, result)
EasyLog(“The default dataset has been saved!”)
else
EasyLog(“An error occurred while saving the default dataset: %q!”, result)
end
end
[/lua]
Use this one when giving it out, please. It’s more complete, and it works because you use ply when you should use self. :v:
[lua]function EasyLog(s, …)
local ns = string.format(s, …)…"
"
Msg(ns)
ServerLog(ns)
if string.match(s, “error”) then ErrorNoHalt(ns) end
end

require"glon"
function _R.Player:Save()
file.Write(“PlayerMoney/”…self:UniqueID()…".txt", self.Money)
self:ChatPrint(“Your money has been saved!”)
EasyLog("%q’s (%s) money has been saved!", self:Name(), self:UniqueID())
end
function _R.Player:Load()
self.Money = tonumber(file.Read(“PlayerMoney/”…self:UniqueID()…".txt") or file.Read(“PlayerMoney/default.txt”) or 0)
self:ChatPrint(“Your money has been loaded!”)
EasyLog("%q’s (%s) money has been loaded!",self:Name(), self:UniqueID())
end
function SetDefault(money)
file.Write(“PlayerMoney/default.txt”, money)
end[/lua] will do for money alone.

Might I suggest also using GLO- oh wait you already did! Man that was a close one. By the way, using every alternate syntax that nobody ever uses does not actually make you a good programmer. It also makes it harder to read!

What do you consider as “alternate syntax” in that snippet of code?

I’ve adopted that style, and I like it. If you don’t like it then you can GTFO.

The complete lack of spacing for one thing, everything’s compacted together and is difficult to follow. I lol’d at the “GTFO” request, take a hint. Your code is ugly.

Dear lord I do not like that he uses pointless syntax macros in his code. I best flee the premises immediately.

Why use self instead? I do the same thing and use Ply…

Go learn Lua.

In the instance of that snippet, ‘ply’ isn’t defined anywhere. ‘self’ is only defined due to the function being defined with : instead of .
If it were to be defined:
[lua]
function _R.Player.Save(ply)

[/lua]
Then yes, ply would be correct.

Thanks Lex, didn’t spot that out :wink:

O then i am using it correctly… :slight_smile:

edit:
just to clear things up i thought that applied to all scripts or something so thats why i was asking

Thanks guys :slight_smile: I worked for at least five hours yesterday trying to figure this out. I am still new to Lua, this has taught me a lot.

GLON is never needed.

Of course GLON is never needed, but the need to serialise data appears very often.
GLON offers a simple, easy to use, yet powerful and dynamic solution.

Sorry for bringing up this old thread…
But however, I have got a question concerning the script…

How do I use the save function?

Do I use “Save()”?

Or how?
I only get errors when trying this v-v

Instead of rating me dumb, how about helping me?

Oh yeah, forgot, the true spirit of FP, is rating people “dumb” because they need some help.
Yeah, thats right.
Furry’s everywhere >:c

No but seriously, that is the true spirit of FP.
Have no intention to help out anyone.

Sorry if this has been asked before, but what is the _R table?
I’ve seen it once before when I looked for all globals which started with _, but it appeared to be exactly the same as _G and _E when I ran PrintTable on it.

Iirc _G is the global table, so if you do MyVar = 1 then _G[“MyVar”] is also 1, and _R i think is for the player.

_R is a table of metatables.

[lua]print(_R.Player == FindMetaTable(“Player”))[/lua]
prints ‘true’.

Look into using PData… much easier to use to save data on the server.