Jump to content
  • 0

Storing in SQL database cash of a player on logout


AlexMiller

Question

Hi there, i'd like to have a databse table and, on player login with resource "logingui" ( i think with the exported event "clientLoginSuccess" but i'm not sure ) the server gives the player the cash the player has the last time he logged in. I did this script but it doesn't work... :

function aggiorna (source)
local sourcemoney = getPlayerMoney ( source )
result2 = executeSQLQuery("SELECT cash FROM cashes WHERE player=?", source )
executeSQLQuery("UPDATE cashes SET cash=? WHERE player='".. source .."'", sourcemoney )
outputChatBox ( "Money updated", source ) 
 
end
addEventHandler ( "onPlayerQuit", getRootElement(), aggiorna )
 
 
function daisoldi (source)
local result = executeSQLSelect ( "cashes", "cash", "player = '" .. source .. "'" )
givePlayerMoney ( source, result[1].cash )
 
end
addEventHandler ( "clientSuccessLogin", getRootElement(), daisoldi )
 
function creatavola ( )
executeSQLQuery("CREATE TABLE IF NOT EXISTS cashes ( cash NUMERIC, player TEXT )" )
local source = getLocalPlayer
outputChatBox ( "SQL Table successfully created" , source ) 
 
end
addEventHandler ( "onResourceStart", getRootElement(), creatavola )

For non-italia readers ( 98% of forum members xD ), "aggiorna" means refresh. "daisoldi" means givemoney . "creatavola" means createtable.

The table is created but when it have to refresh player money i get this error:

' attempt to concatenate a local 'source' ( a userdata value ) '

Thank you.

Link to comment

4 answers to this question

Recommended Posts

  • 0

You're trying to put a player into a string. That will, obviously, not work. use getAccount on a player to get the account he's logged into, and getAccountName to get that account's name in a string (text) format.

And don't code in Italian, it makes trying to read your code hell.

Link to comment
  • 0

There's nothing wrong with SQL, but if you want to use accounts, it might be a better idea to use the account functions instead.

In this way, instead of constantly querying SQL, you can simply do getAccountData to retrieve something and setAccountData to set it. Simple as that. The only issue here is that you need to get the player's account. You do this with, guess what, getPlayerAccount. (It's not in the list here, because it's classified as a "player" function instead of an account one, due to its name)

Because it's simply easier to do...

local acc=getPlayerAccount(player)
if not isGuestAccount(acc) then      -- Make sure he actually logged in, it's kinda useless otherwise
setAccountData(acc, "cash", 9001) -- It's over 9000! 
setAccountData(acc, "cheeze", "is spelled wrong")
end
 
-- Btw, in case you didn't know, you can remove the pieces of text after the --, they're just comments

...than your code. SQL is just more useful if you want to store something non-related to accounts. (i.e. race times, vehicle data etc)

Link to comment
  • 0

I used your example and i used account's data. That's what i made :D :

function onPlayerQuit3 ( )
local account = getPlayerAccount ( source )
if ( account ) then
local money = getPlayerMoney ( source )
local health = getElementHealth ( source )
setAccountData ( account, "rpg.money", money )
if ( health == 0 ) then 
setAccountData ( account, "rpg.health", 100 )
else 
setAccountData ( account, "rpg.health", health )
end
end
end
 
function onPlayerJoin3 ( )
local account = getPlayerAccount ( source )
if ( account ) then
local money = getAccountData ( account, "rpg.money" )
local health = getAccountData ( account, "rpg.health" )
if ( money ) then
setPlayerMoney ( source, money )
end
if ( health ) then
setElementHealth ( source, health )
end
end
end
 
addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit3 )
addEventHandler ( "onPlayerLogin", getRootElement ( ), onPlayerJoin3 )

I tested it and i works, thank you Gamesnert for that, and robhol, for your lua's guide. (i started learning lua from your guide :D)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...