(Money) Profile Loading Error

I’m trying to make a simple money system, and the money increases alright, and saves alright, but when I reconnect to my server it resets the values to 0, without loading the .txt files again.

This is the entire save/load section of the script. I don’t know what else to do. The script doesn’t want to load .txt files, and I have no idea why.

[lua]timer.Create(“SAddUp”,1,0,function()
for k,v in pairs(player.GetAll()) do
v:SetNWInt(“Silver”,v:GetNWInt(“Silver”)+1)
end
end)

timer.Create(“GAddUp”,100,0,function()
for k,v in pairs(player.GetAll()) do
v:SetNWInt(“Gold”,v:GetNWInt(“Gold”)+1)
end
end)

timer.Create(“SaveProfileS”,30,0,function()
for k,v in pairs(player.GetAll()) do
file.Write(“QUICKHUD/Currency/” … v:UniqueID() … “_gold.txt”, “Gold: “…v:GetNWInt( “Gold” ))
end
print(”(SILVER) Profile Saved.”)
end)

timer.Create(“SaveProfileG”,30,0,function()
for k,v in pairs(player.GetAll()) do
file.Write(“QUICKHUD/Currency/” … v:UniqueID() … “_gold.txt”, “Gold: “…v:GetNWInt( “Gold” ))
end
print(”(GOLD) Profile Saved.”)
end)

function SaveOnLeave(pl)
file.Write(“QUICKHUD/Currency/” … pl:UniqueID() … “_silver.txt”, "Silver: "…pl:GetNWInt( “Silver” ))
file.Write(“QUICKHUD/Currency/” … pl:UniqueID() … “_gold.txt”, "Gold: "…pl:GetNWInt( “Gold” ))
end
print(“Profile Saved.”)
hook.Add(“PlayerDisconnected”, “PlayerDisconnect”, SaveOnLeave)

function SaveOnDown()
for k, v in pairs(player.GetAll()) do
file.Write(“QUICKHUD/Currency/” … pl:UniqueID() … “_silver.txt”, "Silver: "…pl:GetNWInt( “Silver” ))
file.Write(“QUICKHUD/Currency/” … pl:UniqueID() … “_gold.txt”, "Gold: "…pl:GetNWInt( “Gold” ))
end
print(“Profile Saved.”)
end
hook.Add(“ShutDown”, “ServerShutDown”, SaveOnDown)

function Load(pl)
if pl:IsValid() then
if file.Exists(“QUICKHUD/Currency”… pl:UniqueID() …".txt") then
pl:SetNWInt(“Silver”, file.Read(“QUICKHUD/Currency/” … pl:UniqueID() … “_silver.txt”))
pl:SetNWInt(“Gold”, file.Read(“QUICKHUD/Currency/” … pl:UniqueID() … “_gold.txt”))
print(“Profile Loaded.”)
else
file.Write( “QUICKHUD/Currency/” … pl:UniqueID() … “_silver.txt”, "Silver: "…pl:GetNWInt( “Silver” ))
file.Write( “QUICKHUD/Currency/” … pl:UniqueID() … “_gold.txt”, "Gold: "…pl:GetNWInt( “Gold” ) )
print(“Profile Created.”)
end
end
end
hook.Add(“PlayerInitialSpawn”, “LoadOnSpawn”, Load)[/lua]

Well, your issue is when you load it you only set the values if


file.Exists("QUICKHUD/Currency".. pl:UniqueID() ..".txt")

is true. However, you never create that particular file in the “else” statement after it, so there’s your problem.

There’s a bigger issue at hand: this is a pretty inefficient way of doing it. You should use SQL. For example,

[lua]function Initialize()
sql.Query(“CREATE TABLE IF NOT EXISTS player_resources( uniqueid INTEGER NOT NULL, gold INTEGER NOT NULL, silver INTEGER NOT NULL, PRIMARY KEY (uniqueid) )”)
end

function GetResource( pl, key )
sql.QueryValue( “SELECT " … sql.SQLStr(key) … " FROM player_resouces WHERE uniqueid=” … pl:UniqueID())
end[/lua]

For a better SQL example relating to money, see here: http://entoros.pastebin.com/Yk2cX3b4

Note entirely relevant, however you can combine your silver and gold values together. Assuming 100 silver equals 1 gold, you can do simple division to find out the values of gold (134 silver = 1 gold and 34 silver). It would make math operations a bit easier, and you need one less text file.

[lua]local money = ply:GetNWInt(“money”)
local silver = money%100
local gold = (money-silver)/100
print(gold, silver)[/lua]