weapon saving.

Hi! I was wondering if any of you dashing people out there would help me with something, you know I’ll return the favor! ;D

I want to make a weapon saving system, you spawn into a server, with fist or whatever. You then get weapons. You lag out of the server( because we know you would never voluntarily leave meh) and your weapons are gone! What if there was a weapon saving system? You spawn, get weapons, leave, come back, and have your weapons? But when you die, you lose your weapons. I know this was done in Stranded. How could I do this for my game? Btw, data saving is my worst subject D: Thanks in advance guys! :smiley:

  1. Decide when to save the player’s weapons (probably on disconnect)
  2. Call a function when they die that gets and saves their weapons
  3. Decide how you’re going to be saving the data (simplest is probably the file library)
  4. When people join, load their data, then process it, and give them the corresponding weapons
  5. Create another function for when players die to remove their weapon data

See, I know this, just not how to do it. I’m trying to get some further assistance, not spoon fed, but just enough to help :smiley:

Ah. Well, those links really should give you what you need. If not, more explanation:
Like I said, 1. hook for file saving:
[lua]
local function getsavedweapons(ply)
if not file.IsDir(‘weaponsave’, ‘DATA’) then
file.CreateDir(‘weaponsave’)
end

local plyweapons

local filename = string.Replace(ply:SteamID(), ':', '_')

if not file.Exists('weaponsave/' .. filename .. '.txt', 'DATA') then
	plyweapons = {}
else
	local data = util.JSONToTable(file.Read('weaponsave/' .. filename .. '.txt', 'DATA'))
	
	plyweapons = data or {}
end

return plyweapons

end

local function setsavedweapons(ply)
if not file.IsDir(‘weaponsave’, ‘DATA’) then
file.CreateDir(‘weaponsave’)
end

local filename = string.Replace(ply:SteamID(), ':', '_')

local plyweptable = {}
for _, wep in pairs(ply:GetWeapons()) do
	plyweptable[#plyweptable+1] = wep:GetClass()
end

file.Write('weaponsave/' .. filename .. '.txt', util.TableToJSON(plyweptable))

end

hook.Add(“PlayerDisconnected”, “SavePlayerWeapons”, setsavedweapons)
[/lua]
(Untested code, copied from pointshop)
Then have the death hooks with file.Delete and a join hook for file.Read.
If you still need help, this should be VERY helpful.

[editline]woo[/editline]
I edited my old simpler code out. Here it is though:
[lua]
hook.Add(“PlayerDisconnected”, “SavePlayerWeapons”, function(plyr)
local plyrweps = plyr:GetWeapons()
local filename = string.Replace(ply:SteamID(), ‘:’, ‘_’) – from pointshop
file.Write(“weapons/”…filename, util.TableToJSON(plyrweps))
end)
–(Untested code)
[/lua]

I’ll give it a try, data confuses the fuck out of me aha

[lua]
hook.Add(“PlayerInitialSpawn”, function(ply)
local weapons = file.Read(“saveweapons/”…string.gsub(ply:SteamID(), “:”, “”)…".txt", “DATA”)
weapons = string.Explode(" ", weapons)
for k,v in pairs(weapons) do
ply:Give(v)
end
end)

hook.Add(“PlayerDisconnect”, function(ply)
local weapons = {}
for k,v in pairs(ply:GetWeapons()) do
weapons[k] = v:GetClass()
end
weapons = table.concat(weapons, " “)
if !file.IsDir(“saveweapons”, “DATA”) then
file.CreateDir(“saveweapons”)
end
file.Write(“saveweapons/”… string.gsub(ply:SteamID(), “:”, “”) …”.txt", weapons)
print(“Saved weapons of “… ply:Nick() …”(”…string.gsub(ply:SteamID(), “:”, “”)…")")
end)
[/lua]

Author has an important point of using weapon:GetClass on the weapons. Also the usage of table.concat and string.explode is probably better in this case than tabletojson/back.

How to do the remove weapons on death? I’ve tried different ways, but they don’t work.

Hmmm… Try, upon spawn, (not initial spawn, but like the 2nd spawn which would be after you die) to have it strip the player and then immediately save weapons.

[editline]edit[/editline]
Why is it dumb? :tinfoil:

Here’s another example: https://dl.dropboxusercontent.com/u/26074909/tutoring/_systems/player_save_and_restore_weapons.lua.html

If you don’t understand this pseudo code you won’t simple built something…

[lua]
hook.Add(“PlayerDeath”, function(victim, inf, attacker)
if file.Exists(“saveweapons/”… string.gsub(victim:SteamID(), “:”, “”)…".txt") then
file.Delete(“saveweapons/”… string.gsub(victim:SteamID(), “:”, “”)… “.txt”)
end
end)
[/lua]

I’m new to data saving man, give me a break D;

Make 2 functions 1. To save the weapon onto database and 2. To load the weapon. Call the load weapon at InitialSpawn and SaveWeapon when he disconnects/

I did something recently if you’re still interesed

http://forum.facepunch.com/showthread.php?t=1428080

I pretty much wrote the whole script for you… just copy and paste it, and tell me if you do get any errors, which you’re not suppose to.

[lua]
hook.Add(“PlayerInitialSpawn”, function(ply)
local weapons = file.Read(“saveweapons/”…string.gsub(ply:SteamID(), “:”, “”)…".txt", “DATA”)
weapons = string.Explode(" ", weapons)
for k,v in pairs(weapons) do
ply:Give(v)
end
end)

hook.Add(“PlayerDisconnect”, function(ply)
local weapons = {}
for k,v in pairs(ply:GetWeapons()) do
weapons[k] = v:GetClass()
end
weapons = table.concat(weapons, " “)
if !file.IsDir(“saveweapons”, “DATA”) then
file.CreateDir(“saveweapons”)
end
file.Write(“saveweapons/”… string.gsub(ply:SteamID(), “:”, “”) …”.txt", weapons)
print(“Saved weapons of “… ply:Nick() …”(”…string.gsub(ply:SteamID(), “:”, “”)…")")
end)

hook.Add(“PlayerDeath”, function(victim, inf, attacker)
if file.Exists(“saveweapons/”… string.gsub(victim:SteamID(), “:”, “”)…".txt") then
file.Delete(“saveweapons/”… string.gsub(victim:SteamID(), “:”, “”)… “.txt”)
end
end)
[/lua]

Because you could just use the PlayerDeath hook instead of the spawn hook, and there would be no point of stripping their weapons, they would obviously loose them when they die…