My first meta function experiment, problems.

I´m currently experimenting a bit with meta functions, I have never used it before but am quite interested in learning how to add some simple commands like AddExperience(‘amount’).

I’m using SQLite to retrieve the information.

I’ve currently setup a test table in the SQLite database called “PlayerData”.

PlayerData:
[SteamID] [WalkExperience]

The inserting of the players SteamID works fine, default WalkExperience is 0.

[lua]

function NewPlayer(ply)

local SID = ply:SteamID()
local querystring = “INSERT INTO PlayerData(SteamID, WalkExperience) VALUES(’”…SID…"’, ‘0’)"

sql.Query(querystring)

end
hook.Add(“PlayerAuthed”, “NewPlayer”, NewPlayer)

[/lua]

Now my AddExperience meta function.

[lua]

local CSN = FindMetaTable(“Player”)

function CSN:RetrieveExperience()
local SID = self:SteamID()
local EXP = sql.QueryValue(“SELECT WalkExperience FROM PlayerData WHERE SteamID = '”…SID…"’")

return tonumber(EXP)

end

function CSN:AddExperience(amount)
local SID = self:SteamID()
local CurrentEXP = self:RetrieveEXP()
local NewEXP = CurrentEXP + amount

sql.Query(“UPDATE PlayerData SET WalkExperience = '”…NewEXP…"’ WHERE SteamID = ‘"…SID…"’")

end

[/lua]

And my test script hooked on PlayerDeath which would add 10 experience.

[lua]

function TestMetaFunctions(ply)
ply:AddExperience(10)
end
hook.Add(“PlayerDeath”, “TestMetaFunctions”, TestMetaFunctions)

[/lua]

As I said, I’ve never touched these meta functions, and this is an experiment. But I am keen on learning how to use them, from what I’ve seen they are very handy.

Could someone give me out a few pointers, or am I like doing things totally wrong. :frowning:

Edit:

Noticed an error in my script. :open_mouth:

[editline]04:56PM[/editline]

Sorry guys got it working. :slight_smile: