Jump to content

SQLite Save System Problem


Wisam

Recommended Posts

Hey, here is a sqlite save system which saves the player's basic stuff such as position, dem, int, money, skin..etc and it works perfectly expect when i decided to add weapons save too cause i can't add the script to the server without it including weapons save.. well i've been struggling to sort it out but nothing worked so far, and i posted it here so please if you know anything that could help post here

Thanks you in advance.

Script:

  
local isInSaveDB = {} 
local db = dbConnect("sqlite", "/playerstats.db") 
dbExec(db, "CREATE TABLE IF NOT EXISTS saving (account TEXT, model TEXT, x TEXT, y TEXT, z TEXT, Wanted TEXT, interior TEXT, dimension TEXT, team TEXT, rotation TEXT, money TEXT, Occupation TEXT, Weapon TEXT)") 
  
function convertWeaponsToJSON(player) 
    local weaponSlots = 12 
    local weaponsTable = {} 
    for slot=1, weaponSlots do 
        local weapon = getPedWeapon( player, slot ) 
        local ammo = getPedTotalAmmo( player, 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 loadData(query) 
        local d = dbPoll(query, 0) 
         
        if (not d) then return end 
        for ind, data in ipairs(d) do 
                isInSaveDB[data.account] = {data.model, data.x, data.y, data.z, data.Wanted, data.interior, data.dimension, data.team, data.rotation, data.money, data.Occupation, data.Weapon} 
        end 
end 
dbQuery(loadData, db, "SELECT * FROM saving") 
  
function setData(plr) 
     
        local account = getAccountName(getPlayerAccount(plr)) 
        if (account ~= "guest") then 
         
                local team = getPlayerTeam(plr) 
                if (team) then 
                        team = getTeamName(team) 
                end 
                local weapons = convertWeaponsToJSON(plr) 
                local model = getElementModel(plr) 
                local Wanted = getElementData(plr,"Wanted") or 0 
                local money = getPlayerMoney(plr) 
                local x, y, z = getElementPosition(plr) 
                local rotation = getPedRotation(plr) 
                local occupation = getElementData(plr, "Occupation") 
                local interior, dimension = getElementInterior(plr), getElementDimension(plr) 
         
                if (isInSaveDB[account]) then 
                        dbExec(db, "UPDATE saving SET model=?, x=?, y=?, z=?, Wanted=?, interior=?, dimension=?, team=?, rotation=?, money=?, Occupation=?, Weapon=? WHERE account=?", tostring(model), tostring(x), tostring(y), tostring(z), tostring(wanted), tostring(interior), tostring(dimension), tostring(team), tostring(rotation), tostring(money), tostring(occupation), tostring(account), tostring(weapons)) 
                        isInSaveDB[account] = {model, x, y, z, Wanted, interior, dimension, team, rotation, money, occupation, weapons} 
                else 
                        dbExec(db, "INSERT INTO saving (account, model, x, y, z, Wanted, interior, dimension, team, rotation, money, Occupation, Weapon) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", tostring(account), tostring(model), tostring(x), tostring(y), tostring(z), tostring(wanted), tostring(interior), tostring(dimension), tostring(team), tostring(rotation), tostring(money), tostring(occupation), tostring(weapons)) 
                        isInSaveDB[account] = {model, x, y, z, Wanted, interior, dimension, team, rotation, money, occupation, weapons} 
                end 
        end 
end 
  
function executeData() 
  
        local account = getAccountName(getPlayerAccount(source)) 
         
        if (isInSaveDB[account] and account ~= "guest") then 
                local model = isInSaveDB[account][1] 
                local x, y, z = isInSaveDB[account][2], isInSaveDB[account][3], isInSaveDB[account][4] 
                local Wanted = isInSaveDB[account][5] 
                local interior = isInSaveDB[account][6] 
                local dimension = isInSaveDB[account][7] 
                local team = isInSaveDB[account][8] 
                local rotation = isInSaveDB[account][9] 
                local money = isInSaveDB[account][10] 
                local Occupation = isInSaveDB[account][11] 
                local weapons = isInSaveDB[account][12] 
                 
                giveWeaponsFromJSON(source, weapons) 
                setPlayerMoney(source, tonumber(money)) 
                setPlayerWantedLevel(source, Wanted) 
                setPlayerTeam(source, team) 
                spawnPlayer(source, x, y, z, rot, model, int, dim) 
                setElementModel(source, model) 
                setElementData(source, "Occupation", tostring(Occupation)) 
                setCameraTarget(source, source) 
         
             --[[   local city = exports.CRmisc:formatCity(source) 
                if (lvBanned[source]) then 
         
                        if (city ~= "LV") then 
                                setElementPosition(source, 1606.26, 1845.99, 10.82) 
                        end 
                        outputChatBox("Your ban will expire in "..lvBanned[source][2].." days", source, 255, 255, 0, "default-bold", true, 0.1) 
                end 
            --]] 
        end 
end 
addEventHandler("onPlayerLogin", root, executeData) 
  
function runDBSave() 
        for ind, plr in pairs(getElementsByType("player")) do 
                setData(plr) 
        end 
end 
setTimer(runDBSave, 50, 0) 
  
  

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