Jump to content

[HELP] SQLite save system


Wisam

Recommended Posts

Hey.. so i got here a sqlite save system made by my friend that saves the player's basic stats such as money, weapon stas, skin, location...etc

But my friend is too busy with the other scripts we are preparing and he said he can finish it later but i posted here because its an important script and i need it really soon before opening the server, also i don't want him to do all the work.. so ill post the script and i hope you can help me :)

And here's what i did to test it:

i started the script > joined the server > got my self weapons, money and weapon stats > i left the server and joined again > logged in , and i got nothing

Here's what i got in the debug when Quiting the server:

Sqlite_savesystem\server.lua:36: bad argument #1 to 'paris' (table expeced, got string)

  
database = dbConnect( "sqlite", "datas.db" )-- connectDB 
skills =  
{   
    [ 69 ] = 22, 
    [ 70 ] = 23, 
    [ 71 ] = 24, 
    [ 72 ] = 25, 
    [ 73 ] = 26, 
    [ 74 ] = 27, 
    [ 75 ] = 28, 
    [ 76 ] = 29, 
    [ 77 ] = 30, 
    [ 78 ] = 31, 
    [ 79 ] = 34, 
} 
  
if database then 
    outputDebugString ('Connect') 
    dbExec( database, "CREATE TABLE IF NOT EXISTS `accounts` ( `serial` text, `x` float, `y` float, `z` float, `intterior` int(5), `demension` int(5), `skin` int(3), `money` int(9), `health` float, `armor` float, `wanted` int(1), `guns` text)" 
) 
else 
    outputDebugString ("Trouble") 
end 
  
function getWeaponSlot( slot ) 
    for i, v in pairs( skills ) do 
        if v == slot then 
            return v   
        end 
    end 
end 
  
function convertJSONToString( player ) 
    local string = "" 
    for i, v in pairs( player ) do 
        if string == "" then 
        string = string .. tostring( i ).. ":" .. tostring( v ).. "," 
    end end 
end 
  
function convertStringToJson( player ) 
    local json = { } 
    local sp = split( player, "," ) 
    for i, v in ipairs( sp ) do 
        local sp2 = split( v, ":" ) 
        json[ sp2[1] ] = sp2[2] 
    end 
end 
  
function convertWeaponsToJSON(player) 
    local weaponSlots = 12 
    local weaponsTable = {} 
    for slot=1, weaponSlots do 
        local weapon = getPedWeapon( source, slot ) 
        local ammo = getPedTotalAmmo( source, slot ) 
        if (weapon > 0 and ammo > 0) then 
            weaponsTable[weapon] = ammo 
        end 
    end 
    return toJSON(weaponsTable) 
end 
  
function giveWeaponsFromJSON(player, weapons) 
    if (weapons and weapons ~= "") then 
        for weapon, ammo in pairs(fromJSON(weapons)) do 
            if (weapon and ammo) then 
                giveWeapon(player, tonumber(weapon), tonumber(ammo)) 
            end 
        end 
    end 
end 
  
function saveAccounts () -- Save in the database 
    local serial = getPlayerSerial ( source ) 
    local x,y,z = getElementPosition( source ) 
    local i = getElementInterior( source ) 
    local d = getElementDimension( source ) 
    local skin = getElementModel ( source ) 
    local money = getPlayerMoney ( source ) 
    local health = getElementHealth ( source ) 
    local armor = getPedArmor ( source ) 
    local wanted = getPlayerWantedLevel ( source ) 
    local weapons = convertJSONToString( convertWeaponsToJSON( source ) ) 
    local q =  dbQuery(database,"SELECT * FROM `accounts` WHERE `serial` = '".. serial .."'") 
    local q = dbPoll( q, -1 ) 
    if ( #q == 0 ) then 
        dbExec( database, "INSERT INTO accounts ( `serial` , x, y, z, intterior, demension, skin, money, health, armor, wanted, guns ) VALUES ( '" .. serial .. "', " .. x .. ", " .. y .. ", " .. z .. "," .. i .. ", " .. d .. "," .. skin .. "," .. money .. ", ".. health ..", ".. armor ..", " .. wanted .. ", " .. weapons .. " )" ) 
    else 
        res = dbExec ( database, "UPDATE `accounts` SET x = ".. x ..", y =  ".. y ..", z = ".. z ..", intterior = ".. i ..", demension = ".. d ..", skin = ".. skin ..", money = ".. money ..", health = ".. health ..", armor = ".. armor ..", wanted = ".. wanted ..", guns = " .. weapons .." WHERE `serial` = '"..serial.."'") 
    end 
end 
  
function loadAccounts () -- Loading from the database 
    local serial = getPlayerSerial ( source ) 
    local result = dbPoll( dbQuery(  database ,"SELECT * FROM `accounts` WHERE `serial` = '"..serial.."'" ), -1 ) 
    if #result == 1 then 
        while true do 
            local row = result[ 1 ] 
            outputChatBox ('Server has loaded your last location when you quit. @Build-By-Anubhav', source, 0, 255, 0, true) 
            setElementPosition ( source, row.x, row.y, row.z) 
            setElementInterior ( source, row.intterior ) 
            setElementDimension ( source, row.demension ) 
            setElementModel ( source, row.skin ) 
            setPlayerMoney ( source, row.money ) 
            setElementHealth ( source, row.health) 
            row.guns = convertStringToJson( row.guns ) 
            giveWeaponsFromJSON( source, row.guns ) 
            setPedArmor ( source, row.armor ) 
            setPlayerWantedLevel ( source, row.wanted ) 
            break 
        end 
    end 
end 
addEventHandler ( "onPlayerSpawn", getRootElement(), loadAccounts ) 
addEventHandler ( "onPlayerQuit", getRootElement(), saveAccounts ) 
  
  
function onQuit ( ) 
    local account = getPlayerAccount ( source ) 
    if ( isGuestAccount ( account ) ) then 
        return 
    end 
  
    setAccountData ( account, "stat22", tostring ( getPedStat ( source, 69 ) ) ) 
    setAccountData ( account, "stat23", tostring ( getPedStat ( source, 70 ) ) ) 
    setAccountData ( account, "stat24", tostring ( getPedStat ( source, 71 ) ) ) 
    setAccountData ( account, "stat25", tostring ( getPedStat ( source, 72 ) ) ) 
    setAccountData ( account, "stat26", tostring ( getPedStat ( source, 73 ) ) ) 
    setAccountData ( account, "stat27", tostring ( getPedStat ( source, 74 ) ) ) 
    setAccountData ( account, "stat28", tostring ( getPedStat ( source, 75 ) ) ) 
    setAccountData ( account, "stat29", tostring ( getPedStat ( source, 76 ) ) ) 
    setAccountData ( account, "stat30", tostring ( getPedStat ( source, 77 ) ) ) 
    setAccountData ( account, "stat31", tostring ( getPedStat ( source, 78 ) ) ) 
    setAccountData ( account, "stat32", tostring ( getPedStat ( source, 79 ) ) ) 
end 
addEventHandler ( "onPlayerQuit", getRootElement(), onQuit ) 
  
function playerLogin ( _, account ) 
    local Pistolskill = getAccountData ( account, "stat22" ) 
    if not Pistolskill then return end 
    local SilencedPistolskill = getAccountData ( account, "stat23" ) 
    local DesertEagleskill = getAccountData ( account, "stat24" ) 
    local Shotgunskill = getAccountData ( account, "stat25" ) 
    local SawnOffskill = getAccountData ( account, "stat26" ) 
    local Spaz12skill = getAccountData ( account, "stat27" ) 
    local Uziskill = getAccountData ( account, "stat28" ) 
    local MP5skill = getAccountData ( account, "stat29" ) 
    local AK47skill = getAccountData ( account, "stat30" ) 
    local M4skill = getAccountData ( account, "stat31" ) 
    local Sniperskill = getAccountData ( account, "stat32" ) 
    setPedStat ( source, 69, Pistolskill ) 
    setPedStat ( source, 70, SilencedPistolskill ) 
    setPedStat ( source, 71, DesertEagleskill ) 
    setPedStat ( source, 72, Shotgunskill ) 
    setPedStat ( source, 73, SawnOffskill ) 
    setPedStat ( source, 74, Spaz12skill ) 
    setPedStat ( source, 75, Uziskill ) 
    setPedStat ( source, 76, MP5skill ) 
    setPedStat ( source, 77, AK47skill ) 
    setPedStat ( source, 78, M4skill ) 
    setPedStat ( source, 79, Sniperskill ) 
end 
addEventHandler ( "onPlayerLogin", getRootElement(), playerLogin ) 

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