Jump to content

Stats system


Memory

Recommended Posts

Hi, please, help me to save Stats in SQL. Error in 10 line.

function start () 
executeSQLCreateTable("RaceStats", "serial STRING, nick STRING, RaceCash INT, RaceWins INT, RacePoints INT") 
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), start) 
  
function onPlayerJoinLoadStats() 
    local serial = getPlayerSerial(source) 
    local result = executeSQLSelect ( "RaceStats", "*", "serial = '" .. tostring(serial) .. "'" ) 
    if ( type( result ) == "table" and #result == 0 ) or not result then 
    setElementData(source, "Wins", tonumber(result[1]["RaceWins"]) or 0) 
    setElementData(source, "Points", tonumber(result[1]["RacePoints"]) or 0) 
else 
    executeSQLInsert("RaceStats", "'".. tostring(serial) .."', '0', '0', '0'") 
    end 
end 
addEventHandler("onPlayerJoin", getRootElement(), onPlayerJoinLoadStats) 
  
function onPlayerQuitSaveCash() 
local serial = getPlayerSerial(source) 
executeSQLUpdate("RaceStats", "RaceCash = '"..tonumber(getPlayerMoney(source)).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceWins = '"..tonumber(getElementData(source,"Wins")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RacePoints = '"..tonumber(getElementData(source,"Points")).."'", "serial = '"..tostring(serial).."'") 
end 
addEventHandler("onPlayerQuit", getRootElement(), onPlayerQuitSaveCash) 
addEventHandler("onPlayerLogout", getRootElement(), onPlayerQuitSaveCash) 

Link to comment

Again error, ERROR: [gameplay]\userpanel\Stats.lua:20: Database query failed: table RaceStats has 11 columns but 10 values were supplied

function start () 
executeSQLCreateTable("RaceStats", "serial STRING, nick STRING, RaceCash INT, RaceWins INT, RaceDM INT, RacePoints INT, RaceFirst INT, RaceSecond INT, RaceThird INT, RaceCompletes INT, RaceHunters INT") 
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), start) 
  
function onPlayerJoinLoadStats() 
    local serial = getPlayerSerial(source) 
    local result = executeSQLSelect ( "RaceStats", "*", "serial = '" .. tostring(serial) .. "'" ) 
    if type( result ) == "table" and #result > 0 then 
    setElementData(source, "Wins", tonumber(result[1]["RaceWins"]) or 0) 
    setElementData(source, "DM", tonumber(result[1]["RaceDM"]) or 0) 
    setElementData(source, "Points", tonumber(result[1]["RacePoints"]) or 0) 
    setElementData(source, "First", tonumber(result[1]["RaceFirst"]) or 0) 
    setElementData(source, "Second", tonumber(result[1]["RaceSecond"]) or 0) 
    setElementData(source, "Third", tonumber(result[1]["RaceThird"]) or 0) 
    setElementData(source, "Completes", tonumber(result[1]["RaceCompletes"]) or 0) 
    setElementData(source, "Hunters", tonumber(result[1]["RaceHunters"]) or 0) 
    givePlayerMoney(source, tonumber(result[1]["RaceCash"])) 
else 
    executeSQLInsert("RaceStats", "'".. tostring(serial) .."', '0', '0', '0', '0', '0', '0', '0', '0', '0'") 
    end 
end 
addEventHandler("onPlayerJoin", getRootElement(), onPlayerJoinLoadStats) 
  
function onPlayerQuitSaveCash() 
local serial = getPlayerSerial(source) 
executeSQLUpdate("RaceStats", "RaceCash = '"..tonumber(getPlayerMoney(source)).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceWins = '"..tonumber(getElementData(source,"Wins")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceDM = '"..tonumber(getElementData(source,"DM")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RacePoints = '"..tonumber(getElementData(source,"Points")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceFirst = '"..tonumber(getElementData(source,"First")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceSecond = '"..tonumber(getElementData(source,"Second")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceThird = '"..tonumber(getElementData(source,"Third")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceCompletes = '"..tonumber(getElementData(source,"Completes")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceHunters = '"..tonumber(getElementData(source,"Hunters")).."'", "serial = '"..tostring(serial).."'") 
end 
addEventHandler("onPlayerQuit", getRootElement(), onPlayerQuitSaveCash) 
addEventHandler("onPlayerLogout", getRootElement(), onPlayerQuitSaveCash) 
  

Link to comment

Your table have 11 columns. But you insert only in 10.

executeSQLInsert("RaceStats", "'".. tostring(serial) .."', '0', '0', '0', '0', '0', '0', '0', '0', '0'") 

Should be:

executeSQLInsert( "RaceStats", "'" .. tostring( serial ) .. "', '" .. getPlayerName( source ) .. "', '0', '0', '0', '0', '0', '0', '0', '0', '0'" ) 

executeSQLUpdate("RaceStats", "RaceCash = '"..tonumber(getPlayerMoney(source)).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceWins = '"..tonumber(getElementData(source,"Wins")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceDM = '"..tonumber(getElementData(source,"DM")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RacePoints = '"..tonumber(getElementData(source,"Points")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceFirst = '"..tonumber(getElementData(source,"First")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceSecond = '"..tonumber(getElementData(source,"Second")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceThird = '"..tonumber(getElementData(source,"Third")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceCompletes = '"..tonumber(getElementData(source,"Completes")).."'", "serial = '"..tostring(serial).."'") 
executeSQLUpdate("RaceStats", "RaceHunters = '"..tonumber(getElementData(source,"Hunters")).."'", "serial = '"..tostring(serial).."'") 

It's not rationally use more queries for update that. You can make it in one query. It's more faster. Believe me.

Also it's bad idea for execute sql query when player join, you need make it when player logged in.

And i not recommend you use serial as root of user's data. You need use account as root of.

Link to comment

I have again error. ERROR: [gameplay]\userpanel\Stats.lua:27: attempt to concatenate a nil value.

Don't save stats.

function start () 
executeSQLCreateTable("RaceStats", "serial STRING, nick STRING, RaceCash INT, RaceWins INT, RaceDM INT, RacePoints INT, RaceFirst INT, RaceSecond INT, RaceThird INT, RaceCompletes INT, RaceHunters INT") 
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), start) 
  
function onPlayerJoinLoadStats() 
    local serial = getPlayerSerial(source) 
    local result = executeSQLSelect ( "RaceStats", "*", "serial = '" .. tostring(serial) .. "'" ) 
    if type( result ) == "table" and #result > 0 then 
    setElementData(source, "Wins", tonumber(result[1]["RaceWins"]) or 0) 
    setElementData(source, "DM", tonumber(result[1]["RaceDM"]) or 0) 
    setElementData(source, "Points", tonumber(result[1]["RacePoints"]) or 0) 
    setElementData(source, "First", tonumber(result[1]["RaceFirst"]) or 0) 
    setElementData(source, "Second", tonumber(result[1]["RaceSecond"]) or 0) 
    setElementData(source, "Third", tonumber(result[1]["RaceThird"]) or 0) 
    setElementData(source, "Completes", tonumber(result[1]["RaceCompletes"]) or 0) 
    setElementData(source, "Hunters", tonumber(result[1]["RaceHunters"]) or 0) 
    givePlayerMoney(source, tonumber(result[1]["RaceCash"])) 
else 
executeSQLInsert( "RaceStats", "'" .. tostring( serial ) .. "', '" .. getPlayerName( source ) .. "', '0', '0', '0', '0', '0', '0', '0', '0', '0'" ) 
    end 
end 
addEventHandler("onPlayerJoin", getRootElement(), onPlayerJoinLoadStats) 
  
function onPlayerQuitSaveCash() 
local serial = getPlayerSerial(source) 
executeSQLUpdate("RaceStats", "RaceCash = '"..tonumber(getPlayerMoney(source)).."'", "nick = '"..tonumber(getPlayerName ( source )).."'", "RaceWins = '"..tonumber(getElementData(source,"Wins")).."'", "RaceDM = '"..tonumber(getElementData(source,"DM")).."'", "RacePoints = '"..tonumber(getElementData(source,"Points")).."'", "RaceFirst = '"..tonumber(getElementData(source,"First")).."'", "RaceSecond = '"..tonumber(getElementData(source,"Second")).."'", "RaceThird = '"..tonumber(getElementData(source,"Third")).."'", "RaceCompletes = '"..tonumber(getElementData(source,"Completes")).."'", "RaceHunters = '"..tonumber(getElementData(source,"Hunters")).."'", "serial = '"..tostring(serial).."'") 
end 
addEventHandler("onPlayerQuit", getRootElement(), onPlayerQuitSaveCash) 
addEventHandler("onPlayerLogout", getRootElement(), onPlayerQuitSaveCash) 

Link to comment
function start () 
executeSQLCreateTable("RaceStats", "serial STRING, nick STRING, RaceCash INT, RaceWins INT, RaceDM INT, RacePoints INT, RaceFirst INT, RaceSecond INT, RaceThird INT, RaceCompletes INT, RaceHunters INT") 
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), start) 
  
function onPlayerJoinLoadStats() 
    local serial = getPlayerSerial(source) 
    local result = executeSQLSelect ( "RaceStats", "*", "serial = '" .. tostring(serial) .. "'" ) 
    if type( result ) == "table" and #result > 0 then 
    setElementData(source, "Wins", tonumber(result[1]["RaceWins"]) or 0) 
    setElementData(source, "DM", tonumber(result[1]["RaceDM"]) or 0) 
    setElementData(source, "Points", tonumber(result[1]["RacePoints"]) or 0) 
    setElementData(source, "First", tonumber(result[1]["RaceFirst"]) or 0) 
    setElementData(source, "Second", tonumber(result[1]["RaceSecond"]) or 0) 
    setElementData(source, "Third", tonumber(result[1]["RaceThird"]) or 0) 
    setElementData(source, "Completes", tonumber(result[1]["RaceCompletes"]) or 0) 
    setElementData(source, "Hunters", tonumber(result[1]["RaceHunters"]) or 0) 
    givePlayerMoney(source, tonumber(result[1]["RaceCash"])) 
else 
executeSQLInsert( "RaceStats", "'" .. tostring( serial ) .. "', '" .. getPlayerName( source ) .. "', '0', '0', '0', '0', '0', '0', '0', '0', '0'" ) 
    end 
end 
addEventHandler("onPlayerJoin", getRootElement(), onPlayerJoinLoadStats) 
  
function onPlayerQuitSaveCash ( ) 
    local serial = getPlayerSerial ( source ) 
    executeSQLUpdate ( "RaceStats", "RaceCash = '".. getPlayerMoney ( source ) .."'", "nick = '".. getPlayerName ( source ) .."'", "RaceWins = '".. tonumber ( getElementData ( source, "Wins" ) ) or 0 .."'", "RaceDM = '".. tonumber ( getElementData ( source, "DM" ) ) or 0 .."'", "RacePoints = '".. tonumber ( getElementData ( source, "Points" ) ) or 0 .."'", "RaceFirst = '".. tonumber ( getElementData ( source,"First" ) ) or 0 .."'", "RaceSecond = '".. tonumber ( getElementData ( source, "Second" ) ) or 0 .."'", "RaceThird = '".. tonumber ( getElementData ( source, "Third" ) ) or 0 .."'", "RaceCompletes = '".. tonumber ( getElementData ( source, "Completes" ) ) or 0 .."'", "RaceHunters = '".. tonumber ( getElementData ( source, "Hunters" ) ) or 0 .."'", "serial = '".. tostring ( serial ) .."'" ) 
end 
addEventHandler("onPlayerQuit", getRootElement(), onPlayerQuitSaveCash) 
addEventHandler("onPlayerLogout", getRootElement(), onPlayerQuitSaveCash) 

Try that.

Link to comment
Error remained.

And why bad to save in xml, and how long script can normal work if save in xml?

The script will ever work normally if you save in XML. But XML is not the best way for saving much data because you have to download the file, how if you have a 10mb XML file? XML is for saving little details only.

Link to comment
I have again error. ERROR: [gameplay]\userpanel\Stats.lua:27: attempt to concatenate a nil value.

Don't save stats.

function start () 
executeSQLCreateTable("RaceStats", "serial STRING, nick STRING, RaceCash INT, RaceWins INT, RaceDM INT, RacePoints INT, RaceFirst INT, RaceSecond INT, RaceThird INT, RaceCompletes INT, RaceHunters INT") 
end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), start) 
  
function onPlayerJoinLoadStats() 
    local serial = getPlayerSerial(source) 
    local result = executeSQLSelect ( "RaceStats", "*", "serial = '" .. tostring(serial) .. "'" ) 
    if type( result ) == "table" and #result > 0 then 
    setElementData(source, "Wins", tonumber(result[1]["RaceWins"]) or 0) 
    setElementData(source, "DM", tonumber(result[1]["RaceDM"]) or 0) 
    setElementData(source, "Points", tonumber(result[1]["RacePoints"]) or 0) 
    setElementData(source, "First", tonumber(result[1]["RaceFirst"]) or 0) 
    setElementData(source, "Second", tonumber(result[1]["RaceSecond"]) or 0) 
    setElementData(source, "Third", tonumber(result[1]["RaceThird"]) or 0) 
    setElementData(source, "Completes", tonumber(result[1]["RaceCompletes"]) or 0) 
    setElementData(source, "Hunters", tonumber(result[1]["RaceHunters"]) or 0) 
    givePlayerMoney(source, tonumber(result[1]["RaceCash"])) 
else 
executeSQLInsert( "RaceStats", "'" .. tostring( serial ) .. "', '" .. getPlayerName( source ) .. "', '0', '0', '0', '0', '0', '0', '0', '0', '0'" ) 
    end 
end 
addEventHandler("onPlayerJoin", getRootElement(), onPlayerJoinLoadStats) 
  
function onPlayerQuitSaveCash() 
local serial = getPlayerSerial(source) 
executeSQLUpdate("RaceStats", "RaceCash = '"..tonumber(getPlayerMoney(source)).."'", "nick = '"..tonumber(getPlayerName ( source )).."'", "RaceWins = '"..tonumber(getElementData(source,"Wins")).."'", "RaceDM = '"..tonumber(getElementData(source,"DM")).."'", "RacePoints = '"..tonumber(getElementData(source,"Points")).."'", "RaceFirst = '"..tonumber(getElementData(source,"First")).."'", "RaceSecond = '"..tonumber(getElementData(source,"Second")).."'", "RaceThird = '"..tonumber(getElementData(source,"Third")).."'", "RaceCompletes = '"..tonumber(getElementData(source,"Completes")).."'", "RaceHunters = '"..tonumber(getElementData(source,"Hunters")).."'", "serial = '"..tostring(serial).."'") 
end 
addEventHandler("onPlayerQuit", getRootElement(), onPlayerQuitSaveCash) 
addEventHandler("onPlayerLogout", getRootElement(), onPlayerQuitSaveCash) 

I already wrote error, but she wasn't fixed

Link to comment

Solidsnake14 gave you code where all data used operator 'or' if getElementData returned false.

Also

print( tonumber( false ) or 0 ) -- 0  For example false is value returned by getElementData but operator 'or' appropriated a 0 
print( tonumber( false ) ) -- nil 
  

Link to comment

You can create a single table for players data.

E.g.

(TABLE NAME)ServerPlayers: (TABLE COLUMNS)serial STRING, nick STRING, RaceCashINT, Wins INT

Then you'd insert something in the table this way:

A0B1C2D3E4F5G6H7I8J9K0L1M2N3O4P5Q6, "Anderl", 2049229320, 502

When you need a player wins you select "Wins" where "nick" = NICK_STRING.

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...