I have been working on a Karma system for my new Game Mode, but for some reason this code block does not work. The console on the server and on the client do not spit out any Lua errors. Can any one give me a hand? It would very much appreciated!
Here is the code:
[CODE]
function Karma( victim, weapon, killer)
local KARMA = -4
if killer:IsPlayer() then
tmysql.query("SELECT `Karma` FROM `users` WHERE `uid`='" .. killer.ID .. "'", function ( currentkarma )
Msg("okay we got the current karma and now running the math")
updatedkarma = currentkarma[1] - KARMA
Msg("okay now we update the data base math worked")
end
tmysql.query("UPDATE `users` SET `Karma`='" .. updatedkarma .. "' WHERE `id`='" .. killer.ID .. "'")
Msg("Okay were done here, we updated the karma of the killer to reflect what he or she did. that bastered")
end
end
hook.Add("PlayerDeath", "Karma", Karma)[/CODE]
Bump for help please? Any one?
Try this..
[lua]
function Karma( victim, weapon, killer)
local KARMA = -4
if killer:IsPlayer() then
tmysql.query("SELECT `Karma` FROM `users` WHERE `uid`='" .. killer.ID .. "';", function ( currentkarma )
Msg("okay we got the current karma and now running the math")
updatedkarma = currentkarma[1] - KARMA
Msg("okay now we update the data base math worked")
end)
tmysql.query("UPDATE `users` SET `Karma`='" .. updatedkarma .. "' WHERE `id`='" .. killer.ID .. "';")
Msg("Okay were done here, we updated the karma of the killer to reflect what he or she did. that bastered")
end
end
hook.Add("PlayerDeath", "Karma", Karma)
[/lua]
Although I think tmysql might return the query as a table, but I am unsure.
[editline]1st March 2012[/editline]
And it would have returned a syntax error because you were missing a ')'
Edit: After toying with the code, and doing some googleing, I found out one can not simply do what I am trying to do: updatedkarma = currentkarma[1] - KARMA So now really the question is how do I use arithmetic functions on tables?
[QUOTE=zzaacckk;34935981]Try this..
[lua]
function Karma( victim, weapon, killer)
local KARMA = -4
if killer:IsPlayer() then
tmysql.query("SELECT `Karma` FROM `users` WHERE `uid`='" .. killer.ID .. "';", function ( currentkarma )
Msg("okay we got the current karma and now running the math")
updatedkarma = currentkarma[1] - KARMA
Msg("okay now we update the data base math worked")
end)
tmysql.query("UPDATE `users` SET `Karma`='" .. updatedkarma .. "' WHERE `id`='" .. killer.ID .. "';")
Msg("Okay were done here, we updated the karma of the killer to reflect what he or she did. that bastered")
end
end
hook.Add("PlayerDeath", "Karma", Karma)
[/lua]
Although I think tmysql might return the query as a table, but I am unsure.
[editline]1st March 2012[/editline]
And it would have returned a syntax error because you were missing a ')'[/QUOTE]
Thank you for the reply, I just tested the code and it works! kinda... I get the following errors in console ( server side )
[CODE]Hook 'Karma' Failed: [garrysmod\gamemodes\life\gamemode\sv_modules\system_karma.lua:39] attempt to concatenate global 'updatedkarma' (a nil value)
okay we got the current karma and now running the math
[garrysmod\gamemodes\life\gamemode\sv_modules\system_karma.lua:35] attempt to perform arithmetic on field '?' (a nil value)[/CODE]
Line 39 is:
[CODE]tmysql.query("UPDATE `users` SET `Karma`='" .. updatedkarma .. "' WHERE `id`='" .. killer.ID .. "';")[/CODE]
And line 35 is:
[CODE]updatedkarma = currentkarma[1] - KARMA[/CODE]
I think you are correct when you say it returns it as a table, but that should not be a problem, I could convert the int currentkarma to a table, or just change all the other var's to a table? Am I correct about this?
Are you sure that there is a row for that player already?
Did you do an insert somewhere with the default karma?
[QUOTE=SB2DevTeam;34943239]Are you sure that there is a row for that player already?
Did you do an insert somewhere with the default karma?[/QUOTE]
Thanks for the reply, Yes and no. I went into phpmyadmin and added it. I seem to have fixed the problem with the subtracting of the karma using: updatedKarma = tonumber(currentkarma) - KARMA. Now its just telling me that currentkarma is a NIL value. I know there is a value in the MySQL database because I put on there. So for some reason it's having trouble grabbing the current Karma?
Looks to me like your [lua]
tmysql.query("UPDATE `users` SET `Karma`='" .. updatedkarma .. "' WHERE `id`='" .. killer.ID .. "';")
Msg("Okay were done here, we updated the karma of the killer to reflect what he or she did. that bastered")[/lua] is running before it should, try moving that into your function(currentkarma)
As for your problems with currentkarma, use debug messages to find it exactly what it is, eg [lua]MsgN("CurrentKarma: " ..tostring(currentkarma) )[/lua]
and
[lua]PrintTable(currentkarma)[/lua]
Sorry, you need to Log In to post a reply to this thread.