Problem with point saving/loading system SQL

Hi dudes!

I got few problems with my code… First I asked my cousin, he tried to fix my code but still wont work >,< He just broke it even more… Look down for codes & problems.

activitypoints.lua
[lua]AddCSLuaFile(“autorun/activitypoints_cl.lua”)

function sql_value_stats ( ply )
ply.points = sql.QueryValue("SELECT points FROM activitypoints WHERE unique_id = " … SQLStr( ply:SteamID() ))
end

function saveStat ( ply )
steamID = ply:SteamID()
result = sql.Query("UPDATE activitypoints SET points = “…ply.points…” WHERE unique_id = " … SQLStr( steamID ) )
end

function tables_init()
if !sql.TableExists(“activitypoints”) then
result = sql.Query(“CREATE TABLE activitypoints ( unique_id varchar(255), points int )”)
if (!sql.TableExists(“activitypoints”)) then
MsgAll("SQL ERROR: "… sql.LastError( result ) … "
" )
end
end
end

function new_player( SteamID, ply )
steamID = SteamID
sql.Query( “INSERT INTO activitypoints (unique_id, points)VALUES (” … SQLStr( steamID ) … “, ‘0’)” )
result = sql.Query( "SELECT unique_id, points FROM activitypoints WHERE unique_id = " … SQLStr( steamID ) )
if (!result) then
Msg("Creating player activitypoints failed.
")
end
end

function player_exists( ply )

steamID = ply:SteamID()

result = sql.Query("SELECT unique_id, points FROM activitypoints WHERE unique_id = " .. SQLStr( steamID ))
if (result) then
	sql_value_stats( ply )
else
	new_player( steamID, ply )
end

end

function PlayerInitialSpawn( ply )
timer.Create(“SaveStat”, 10, 0, function() saveStat( ply ) end)
player_exists(ply)
end

function AddSomeCash( ply )
ply.points = ply.points + 100
SendUserMessage( “points”, ply, ply.points )
end

concommand.Add(“cashformeh”, AddSomeCash)
hook.Add( “PlayerInitialSpawn”, “activitypoints_newplayer”, PlayerInitialSpawn )
hook.Add( “Initialize”, “activitypoints_init”, tables_init )
[/lua]

activitypoints_cl.lua
[lua]if (CLIENT) then

usermessage.Hook( "points", function( um ) LocalPlayer().points = um:ReadLong() end )

function HUD()
	client = LocalPlayer()
	client.points = client.points or 0
	if !client:Alive() then return end
	if (client:GetActiveWeapon() == NULL or client:GetActiveWeapon() == "Camera") then return end
	draw.RoundedBox(6, ScrW() - 105, 250, 120, 20, Color(196, 172, 0))
	draw.SimpleText("Points: "..client.points, "ScoreboardText", ScrW() - 100, 250, Color(255,255,255,255))
end

hook.Add("HUDPaint", "ActivityHUD", HUD)

end
[/lua]

Tell me some things…

  1. What does these ply.variablename mean? What kind of variables they are and how they work (Cousin made these…)
  2. When using concmd cashformeh, error: includes/modules/usermessage.lua:15: attempt to index upvalue ‘umsg’ (a nil value)
  3. Saving doesnt really work… Got rid of the errors in that function, but still doesnt work… Dunno does player creating even work, cause when I reconnect, my cash is again 0. Btw, I didnt just copypaste that shit from the wiki sql tutorial! (But looked it pretty much)

Please don’t ask in more then one thread, be patient and wait for a response

Now ontopic,

  1. ply means, the event/hook/function recieved a player(ply) index and you can use any of these variables on it http://wiki.garrysmod.com/?title=Player

  2. not sure

  3. not sure either

Just decided to make a new thread cause old one had bad title and many messages before the main one.

Some debugging… Seems like that ply.variable doesnt work. Tried ply:ChatPrint(ply.points) in savestat function, it gave me 0…

Do

[lua]
ply.points = 0
[/lua]

in your initial spawn hook.

If it still fails… then try

[lua]
ply:GetTable().points
[/lua]

instead of

ply.points

Nothing happened…

Still same problem… Cmon, nobody really knows?

Im not sure it very late but, I think your problem as you stated its zero you get. Is that it’s a nil value.

Try
[lua]
function new_player( SteamID, ply )
ply.points = 0
steamID = SteamID
sql.Query( “INSERT INTO activitypoints (unique_id, points)VALUES (” … SQLStr( steamID ) … “, ‘0’)” )
result = sql.Query( "SELECT unique_id, points FROM activitypoints WHERE unique_id = " … SQLStr( steamID ) )
if (!result) then
Msg("Creating player activitypoints failed.
")
end
end
[/lua]

Since you haven’t actually set there points yet.

No, didnt work… :frowning: