Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not SERVER then return end
- CONFIG = {}
- CONFIG.saveweapons = true -- true if you want to save the player's weapons ans false if you don't
- CONFIG.saveteam = true -- true if you want to save the player's team and false if you don't
- CONFIG.savepos = true -- true if you want to save the player's pos and false if you don't
- CONFIG.savemodel = true -- true if you want to save the player's model and false if you don't
- CONFIG.savehealth = true -- true if you want to save the player's health and false if you don't
- CONFIG.savearmor = true -- true if you want to save the player's armor and false if you don't
- CONFIG.teams = {TEAM_CITIZEN} --team that doesn't use the save spawn/weapon/team
- CONFIG.auto_time = 600 --Time between each auto saves (IN SECONDS)
- CONFIG.print = true -- Print a message in the console when the server as saved the player's data
- CONFIG.dontsavedamage = true -- If the player will not save anything after being damaged (cooldown in Seconds)
- CONFIG.dontsavecooldown = 60 -- see above, it's the cooldown when he will be able to disconnect and has is data saved
- CONFIG.cooldownfinish = "You can now disconnect, your data will be saved." -- sentence when the cooldown is finish
- CONFIG.cooldownstart = "Wait "..CONFIG.dontsavecooldown.." before leaving, else it will not save your data" -- sentence when the cooldown starts
- local plyMeta = FindMetaTable("Player")
- function plyMeta:LoadThings()
- local read = file.Read( "save_module/"..string.lower(game.GetMap()).."/" .. self:SteamID64() .. ".txt", "DATA" )
- if not file.Exists( "save_module", "DATA" ) then file.CreateDir( "save_module", "DATA" ) return end
- if not file.Exists("save_module/"..string.lower(game.GetMap()),"DATA") then file.CreateDir( "save_module/"..string.lower(game.GetMap()), "DATA" ) end
- if not file.Exists("save_module/"..string.lower(game.GetMap()).."/"..self:SteamID64()..".txt", "DATA") then
- local data = {pos = Vector(0,0,0), team = TEAM_MINEUR, weapon = {""}}
- file.Write("save_module/"..string.lower(game.GetMap()).."/"..self:SteamID64()..".txt", util.TableToJSON(data) ) return
- end
- local things = util.JSONToTable(read)
- return things
- end
- function plyMeta:SaveThings()
- if not self:Alive() then return end
- if not file.Exists( "save_module", "DATA" ) then file.CreateDir( "save_module", "DATA" ) end
- if not file.Exists("save_module/"..string.lower(game.GetMap()),"DATA") then file.CreateDir( "save_module/"..string.lower(game.GetMap()), "DATA" ) end
- local weapondata ={}
- local ammodata = {}
- local datapos, datateam, datamodel, dataarmor, datahealth
- if CONFIG.saveweapons then
- for k,v in ipairs(self:GetWeapons()) do
- table.insert(weapondata,k, v:GetClass())
- if v:GetPrimaryAmmoType() != -1 then
- if ammodata[v:GetPrimaryAmmoType()] then
- if ammodata[v:GetPrimaryAmmoType()].ammotype then
- if ammodata[v:GetPrimaryAmmoType()].ammotype == v:GetPrimaryAmmoType() then continue end
- end
- end
- table.insert(ammodata,v:GetPrimaryAmmoType(), {ammotype = v:GetPrimaryAmmoType(), ammonum = self:GetAmmoCount(v:GetPrimaryAmmoType())})
- end
- end
- end
- if CONFIG.savepos then datapos = self:GetPos() end
- if CONFIG.savemodel then datamodel = self:GetModel() end
- if CONFIG.saveteam then datateam = self:Team() end
- if CONFIG.savehealth then datahealth = {hp = self:Health(),maxhp = self:GetMaxHealth()} end
- if CONFIG.savearmor then dataarmor = self:Armor() end
- local data = {pos = datapos, team = datateam, weapon = weapondata, ammo = ammodata, model = datamodel, health = datahealth, armor = dataarmor}
- if self:GetNWBool("savecooldown") then data = {"no"} end
- file.Write("save_module/"..string.lower(game.GetMap()).."/"..self:SteamID64()..".txt", util.TableToJSON(data) )
- end
- hook.Add("PlayerDisconnected", "savethings", function(ply)
- ply:SaveThings()
- end)
- hook.Add("ShutDown", "ShutDownSaveThings", function()
- for k,v in pairs(player.GetAll()) do
- v:SaveThings()
- end
- if CONFIG.print then
- print("[SAVE MODULE]DATA SAVED FOR ALL PLAYERS")
- end
- end)
- hook.Add("PlayerInitialSpawn", "loadthings", function(ply)
- local data = ply:LoadThings()
- if not data then return end
- if data == "no" then return end
- if table.HasValue(CONFIG.teams, data.team) then return end
- timer.Simple(1, function()
- if CONFIG.saveteam then
- ply:SetTeam(data.team)
- ply:ConCommand("say /job "..team.GetName(data.team))
- ply:Spawn()
- end
- if CONFIG.savepos then
- ply:SetPos(data.pos)
- end
- if CONFIG.saveweapons then
- if data.weapon then
- for k,v in pairs(data.weapon) do
- ply:Give(v)
- end
- for k,v in pairs(data.ammo) do
- ply:SetAmmo(v.ammonum,v.ammotype)
- end
- end
- end
- if CONFIG.savemodel then
- ply:SetModel(data.model)
- end
- if CONFIG.savehealth then
- ply:SetMaxHealth(data.health.maxhp)
- ply:SetHealth(data.health.hp)
- end
- if CONFIG.savearmor then
- ply:SetArmor(data.armor)
- end
- end)
- end)
- hook.Add("EntityTakeDamage", "DontSaveWhenDamage", function(target, damage)
- if !CONFIG.dontsavedamage then return end
- if !target:IsPlayer() then return end
- if damage:GetAttacker() == target then return end
- if not (damage:GetAttacker():IsNPC() or damage:GetAttacker():IsPlayer()) then return end
- if target:GetNWBool("savecooldown", false) then
- timer.Adjust("savecooldown"..target:UniqueID(), CONFIG.dontsavecooldown, 1, function() if IsValid(target) then target:PrintMessage(HUD_PRINTTALK,CONFIG.cooldownfinish) target:SetNWBool("savecooldown", false) end end)
- else
- target:SetNWBool("savecooldown", true)
- target:PrintMessage(HUD_PRINTTALK,CONFIG.cooldownstart)
- timer.Create("savecooldown"..target:UniqueID(), CONFIG.dontsavecooldown, 1, function() if IsValid(target) then target:PrintMessage(HUD_PRINTTALK,CONFIG.cooldownfinish) target:SetNWBool("savecooldown", false) end end)
- end
- end)
- hook.Add("PlayerDeath", "removecooldowndeath", function(ply)
- if timer.Exists("savecooldown"..ply:UniqueID()) then timer.Destroy("savecooldown"..ply:UniqueID()) target:SetNWBool("savecooldown", false) end
- end)
- timer.Create("SaveThingsAuto", CONFIG.auto_time, 0, function()
- local time = 0
- for k,v in pairs(player.GetAll()) do
- timer.Simple(time, function()
- v:SaveThings()
- end)
- time = time+0.5
- end
- if CONFIG.print then
- print("[SAVE MODULE]DATA SAVED FOR ALL PLAYERS")
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement