Save data

Ok i made a function that safes a players XP, Level and points for my XP system. Problem is how do i make people load the data everytime they join?


function SaveDataPlayer(victim, weapon, killer)
if victim:IsPlayer() and killer:IsPlayer() then
file.Write("exp/"..killer:UniqueID().."_exppoints.txt", killer:GetNWInt("EXP"))
file.Write("exp/"..killer:UniqueID().."_level.txt", killer:GetNWInt("Level"))
file.Write("exp/"..killer:UniqueID().."_skillpoints.txt", killer:GetNWInt("SkillPoints"))
end
end
hook.Add( "PlayerDeath", "SaveDataPlayer", SaveDataPlayer )

This is where it is meant to safe data.

You would use file.Read. But why not just use PData or MySQL to save that kind of stuff? That would result in ALOT of text files.

Yes i know :slight_smile:

But would it be possible to make it in 3 files. 1 for xp, 1 for lvl and 1 for points or just 1 file in total to load all 3 things.

I will not use MySQL no matter what.

to make them read what should i do?


function LoadData ( pl )
file.Read("exp/..pl:UniqueID().."exppoints.txt", pl:GetNWInt("EXP"))
end

this would load the XP for a player?

First of all that is a horrible way to save.


function SaveDataPlayer(victim, weapon, killer)
    if victim:IsPlayer() and killer:IsPlayer() then
        local t = {
            exp=killer:GetNWInt("EXP"),
            lvl=killer:GetNWInt("Level"),
            sp= killer:GetNWInt("SkillPoints")
        }
        file.Write("exp/"..killer:UniqueID()..".txt", util.TableToJSON(t))
    end
end
hook.Add( "PlayerDeath", "SaveDataPlayer", SaveDataPlayer )

then in your PlayerInitialSpawn hook you do


function LoadDataPlayer (p)
    if file.Exist("exp/"..p:UniqueID() .. ".txt","DATA") then
        local t = util.JSONToTable( file.Read("exp/"..p:UniqueID()..".txt") )
        p:SetNWInt("EXP",t.exp)
        p:SetNWInt("Level",t.lvl)
        p:SetNWInt("SkillPoints",t.sp)

    end
end
hook.Add( "PlayerInitialSpawn", "LoadDataPlayer", LoadDataPlayer )

This may help

Whats wrong with MySQL? Also PData would also be a much better solution to saving you stuff.

I agree it was a horrible way to safe :slight_smile:

Im not used to make anything that should safe. Im more in Derma/HUD :slight_smile:

[editline]11th September 2013[/editline]

Hmm now it wont do all normal commands in my GM:PlayerInititalSpawn…

Join 66.150.164.75:27015

Then you in it should auto select blue team and popup a Derma menu. It doesnt.

I’d reccomend SQLite/MySQL. Not terribly hard to code for. SQLite tutorial that specifically covers your request


function LoadDataPlayer (p)
    if file.Exist("exp/"..p:UniqueID() .. ".txt","DATA") then
        local t = util.JSONToTable( file.Read("exp/"..p:UniqueID()..".txt") )
        p:SetNWInt("EXP",t.exp)
        p:SetNWInt("Level",t.lvl)
        p:SetNWInt("SkillPoints",t.sp)

    end
end
hook.Add( "PlayerInitialSpawn", "LoadDataPlayer", LoadDataPlayer )

I think this conflicts with


function GM:PlayerInitialSpawn ( ply )
		ply:SetGravity ( 1 )
		ply:SetWalkSpeed ( 230 )
		ply:SetRunSpeed ( 320 )
		ply:SetTeam ( 1 )
		umsg.Start("Join", ply)
		umsg.End()
		umsg.Start("TeamDerma", ply)
		umsg.End()
end


No it doesn’t, it should be called before your GM:* function, although you should totally move the code from hooks to GM:* functions if you have this ability.

-snip- I stand corrected.

USE FUCKIN SQLLITE

He already said he would not use MySQL or any SQL for that matter. Stop spamming him with ‘USE SQL’. I agree it would be the better option, but he’s not asking for it. Simple-as. Now take your 5th repost SQL minging out of here.

You may also consider saving when the player disconnects, or on GM:ShutDown ( which is if the server shuts down, map changes, etc ) to cover all bases.

Yeah indeed. I edited hook. Just broke my XP system. So goooood :slight_smile: Can anyone learn me how PData works. Used to make HUD/VGUI and gamemode functions so i never needed to use anything that safes

Use the wikis.

PData is essentially a layer on top of SQL. It provides helper functions, set and get which executes the SQL for you.

Basically you call

[lua]PLAYER:SetPData( nameOfField, Data );
PLAYER:GetPData( nameOfField, DefaultValue );[/lua]

It handles the inserting and retrieval of the data from sv.db/cl.db depending whether or not you call it on the server or the client.

So PData uses a SQL database. Then nope.

So your saying instead of taking the easy way of just using 1 line to save and 1 line to load you would rather use a terrible text file saving system? ok.

I want people without SQLite databases to can use this

I think you’re misunderstanding.

SQLite is saved locally in the server’s sv.db file, located in the root folder.
MySQL requires a web server, which some owners don’t have.