Saving money and steam ID in data

So far I have this set up to save money on my script:

[lua]
function Money()
if not file.Exists(“my_gamemode/Money.txt”) then
file.Write(“my_gamemode/Money.txt”, ply:GetNWInt(“Money”))
end
end
[/lua]
But it is used for every player, and I need it to be set for specific players. How would I add in to save SteamID’s as well?

Try this :

[lua]require(“glon”)

function SaveData()
local Table = {}
if file.Exists(“my_gamemode/money.txt”) then
for k,v in pairs(glon.decode(file.Read(“my_gamemode/money.txt”))) do
Table[k] = v
end
end
for _,v in pairs(player.GetAll()) do
Table[v:SteamID()] = v:GetNWInt(“Money”)
end
file.Write(“my_gamemode/money.txt”,glon.encode(Table))
end

function LoadData()
if file.Exists(“my_gamemode/money.txt”) then
for k,v in pairs(glon.decode(file.Read(“my_gamemode/money.txt”))) do
for _,v in pairs(player.GetAll()) do
if v:SteamID() == k and v:GetNWInt(“Money”) != 0 then
v:SetNWInt(“Money”,v)
end
end
end
end
end[/lua]

Alright, but what is require(“glon”)?

[lua]function Money_Save( ply )
local steamid = string.Replace(ply:SteamID(), “:”, “-”)
print(“Saving player “…ply:Nick()…”'s stats…”)
file.Write(“money/”…steamid…".txt", ply:Name()…"
“…ply:SteamID()…”
"…ply:GetNWInt(“Money”))
end[/lua]

To load it again just use something like this…

[lua]
function Money_Load( ply )
if ply:IsPlayer() then
local steamid = string.Replace(ply:SteamID(), “:”, “-”)
local contents = ‘’
if not file.Exists(“money/”…steamid…".txt") then
print(‘No data’)
Money_Save( ply )
else
print(‘Reading’)
contents = file.Read(“money/”…steamid…".txt")
local values = string.Explode("
", contents)
ply:SetNWString( “Money”, values[3] )
end
end
end
[/lua]

I’d recommend you use SQL rather than text files, it’s much more efficient.

KevKev’s code is better so use his. I prefer using separate text files though just for keeping shit organized.

It make the GLon module avalable, the GLon module compresses a table as small as possible.

Using kevkev’s code is this how I could save the money?
[lua]function GM:PlayerDisconnected(ply)
file.Write(“my_gamemode/Money.txt”, ply:GetNWInt(“Money”))
end[/lua]

Or would it just be
[lua]
function GM:PlayerDisconnected(ply)
SaveData()
end[/lua]

It would be
[lua]
function GM:PlayerDisconnected(ply)
SaveData()
end[/lua]

Saving every players info in one file is stupid and you will feel the effects of it as you get a larger playerbase and as you add in more information.

Use one file per player.

Why one file per player? you would never have to edit the files anyway (Also impossible due to GLon) so why multiple files?

Why read the contents of 10,000 player’s data, when you can just read the 30 that is needed?

Most servers don’t get up to 10,000 players served anyhow.

Any server with an average of > 15 players will within two months.

Not if the game barely reaches 15,000 total players, get your facts straight.

Stop rating yourself.


(User was banned for this post ("Why reply?" - mahalis))

Last year, Killa’s ZS server had an average of 12 players on at a time, and the rank database went online in March-ish. By July, there was 12,000 records even after unused ones had been deleted. This was a SQL DB, but it is the same principle.

This.

I’m not, idiots? You can’t even do that.


(User was banned for this post ("Flaming" - mahalis))

Mine’s close to it and multiple files have more pros than cons for large databases. Backing up the data takes a quite bit longer than if it were just one file and finding players is as simple as searching in the single text file. However, it really depends how much you have to save per player and how many players you have. With enough players and enough data saved then that one file can really become too large to sort through and can be a bit expensive for the gamemode to load/save.

Glon makes the files completely unreadable so you won’t be able to edit the data directly without some sort of function coded in.