Best way to save stuff in this situation?

I want a players weapons to save once he/she purchases them.

There is an entire shop where they can buy stuff.

I want the server to save everything they buy, not just one thing.
I then need a way to retrieve each group of stuff so certain guns go into the primary section and secondarys go into a different section and so on.

What is the best way to do this?
It needs to save all the primarys, secondarys, equipments, attributes the player buys.

i am looking to save it as sv.db but as txt is ok as well.

Anyone going to help?
I really need this to carry on.

MySQL, do it.

I am going to use Sqllite or txt. I cannot bother with mysql.

I want to know the best way to save multiple weapons and how I should save them, by name, special id or class name.

I’m stuck as to the best way to save them, do you have an idea?

Untested, but here you go. It is well documented.

[lua]
–[[
BuySave module by kurozael (http://kurozael.com)
kurozael@gmail.com
–]]

buysave = {
players = {},
categories = {}
};

require(“glon”);

–[[
Register a player as a buysave member. Call this for the player
when he initially spawns if you want them to be apart of the
saving system (PlayerInitialSpawn).
–]]
function buysave.RegisterPlayer(ply)
local data = file.Read(“buysave/”…ply:UniqueID()…".txt");

if (data and data != "") then
	buysave.players[ply] = glon.decode(data);
else
	buysave.players[ply] = {};
end;

-- Make sure the player has a table of items for each category.
for k, v in pairs(buysave.categories) do
	if ( !buysave.players[ply][k] ) then
		buysave.players[ply][k] = table.Copy(v);
	end;
end;

end;

–[[
Save a player’s data. This should be called for the player
whenever you want to save their data. I’d recommend PlayerDisconnected
and maybe every five or so minutes via a timer.
–]]
function buysave.SavePlayer(ply)
if ( buysave.players[ply] ) then
file.Write( “buysave/”…ply:UniqueID()…".txt", glon.encode( buysave.players[ply] ) );
end;
end;

–[[
This function registers a category, e.g: Primary, Secondary, Attributes, Hats, whatever.
You can also specify a table as “defaultItems” which will make players start with those items.
The table should be in the following format.

local defaultItems = {
	["weapon_glock"] = 1,
	["weapon_mp5"] = 2
};

buysave.AddCategory("Weapons", defaultItems);

–]]
function buysave.AddCategory(category, defaultItems)
buysave.categories[category] = defaultItems or {};
end;

–[[
This function gets the list of items in a category for a player.

local items = buysave.GetItems(ply, "Primary");

if ( items["weapon_glock"] ) then
	print("The player has "..items["weapon_glock"].." glock(s)!");
end;

–]]
function buysave.GetItems(ply, category)
if ( buysave.players[ply] and buysave.players[ply][category] ) then
return buysave.players[ply][category];
end;

return {};

end;

–[[
A function to get how many of an item that a player has. The
function will return false if the player has none of them.
–]]
function buysave.HasItem(ply, category, itemName)
if ( buysave.players[ply] and buysave.players[ply][category]
and buysave.players[ply][category][itemName] ) then
if (buysave.players[ply][category][itemName] > 0) then
return buysave.players[ply][category][itemName];
else
buysave.players[ply][category][itemName] = nil;
end;
end;

return false;

end;

–[[
This function gets a player’s data in full. It returns a table
whereby the key is the name of the category, and the value is
a second table containing the list of items a player has for that
category.

local data = buysave.GetData(ply);

if ( data["Primary"]["weapon_glock"] ) then
	print("The player has a glock!");
end;

–]]
function buysave.GetData(ply)
return buysave.players[ply] or {};
end;

–[[
A function to give an item to a player, you can specify how many
to give to the player.

buysave.GiveItem(ply, "Primary", "weapon_glock", 1);

–]]
function buysave.GiveItem(ply, category, itemName, amount)
if ( buysave.players[ply] and buysave.players[ply][category] ) then
buysave.players[ply][category][itemName] = (buysave.players[ply][category][itemName] or 0) + amount;
end;
end;

–[[
A function to take an item from a player. You can specify how many
to take from the player.

buysave.TakeItem(ply, "Primary", "weapon_glock", 1);

–]]
function buysave.TakeItem(ply, category, itemName, amount)
if ( buysave.players[ply] and buysave.players[ply][category] ) then
if ( buysave.players[ply][category][itemName] ) then
buysave.players[ply][category][itemName] = buysave.players[ply][category][itemName] - 1;

		if (buysave.players[ply][category][itemName] <= 0) then
			buysave.players[ply][category][itemName] = nil;
		end;
	end;
end;

end;

–[[
Example categories. Add or remove them as you please.
–]]
buysave.AddCategory(“Primary”)
buysave.AddCategory(“Secondary”);
buysave.AddCategory(“Attributes”);
buysave.AddCategory(“Equipment”);
[/lua]

[editline]6th March 2011[/editline]

[lua]
–[[
Example script, feel free to remove this.
–]]
buysave.AddCategory(“Awards”);

hook.Add(“PlayerInitialSpawn”, “BuySave”, function(ply)
buysave.RegisterPlayer(ply);
end);

hook.Add(“PlayerDeath”, “BuySave”, function(ply)
if ( !buysave.HasItem(ply, “Awards”, “Skull”) ) then
buysave.GiveItem(ply, “Awards”, “Skull”, 1);
end;
end);

hook.Add(“PlayerSpawn”, “BuySave”, function(ply)
if ( buysave.HasItem(ply, “Awards”, “Skull”) ) then
ply:PrintMessage(3, “You have the Skull award, shame you died though.”);
end;
end);

hook.Add(“PlayerDisconnected”, “BuySave”, function(ply)
buysave.SavePlayer(ply);
end);
[/lua]

Wow, very wow.

I’m on my iPhone so I can’t see the full script but looking at what I can, this looks brilliant.

Thank you for this and I’ll try and put it to good use.

Also, is there any simpler way of doing something like this, such as when a player buys a glock.

[lua]
ply:SetPData( “primaryguns”, ply:GetPData(“primaryguns”)…" "…AddedWeapon)
[/lua]

Wouldn’t something like that work, then you can decode it with a string.explode(" ") if i am not mistaken.

The script I just wrote for you will work fine for saving data, and is a lot cleaner than doing it that way :slight_smile: