Jump to content

Vehicles


Recommended Posts

Posted (edited)
function spawnDespawnCar(p, carName) 
    local accName = getAccountName(getPlayerAccount(p)) 
    local carID = getVehicleModelFromName(carName) 
    local result = executeSQLQuery("SELECT * FROM vehicles WHERE accName=? and ID=?", accName, carID) 
    if (not getElementData (p,"System.Vehicle")) then 
        local upgrades = fromJSON(result[1]["upgrades"]) 
        local veh = createVehicle(...) 
        addVehicleUpgrade(veh, upgrades) 
        setElementData (p, "System.Vehicle", veh) 
        outputChatBox("Your car has been spawned!", p, 0, 255, 0) 
    elseif (getElementData (p, "System.Vehicle")) then 
        local nveh = getElementData (p, "System.Vehicle") 
        outputChatBox("Your car has been despawned!", p, 0, 255, 0) 
        local nups = toJSON(getVehicleUpgrades(nveh)) 
        destroyElement(nveh) 
        executeSQLQuery("UPDATE vehicles SET upgrades=? WHERE accName=? AND id=?", nups, accName, carID) 
        setElementData (p, "System.Vehicle", false) 
    end 
end 
addEvent("spawnDespawnCar", true) 
addEventHandler("spawnDespawnCar", root, spawnDespawnCar) 

I'm having problems saving and getting some upgrades of the car to SQLite, I'm quite new to JSON functions and I guess that I'm not getting it right. :P

Edited by Guest
Posted
I didn't post the full code, it is right in my actual code.

Try this

function spawnDespawnCar(carName) 
    local acc = getPlayerAccount(source) 
    if acc and not isGuestAccount(acc) then  
    local accName = getAccountName(acc) 
    local carID = getVehicleModelFromName(carName) 
    local result = executeSQLQuery("SELECT * FROM vehicles WHERE accName=? and ID=?", tostring(accName), tonumber(carID)) 
    if (not getElementData (source,"System.Vehicle")) then 
        local veh = createVehicle(...) 
        local upgrades = fromJSON(result[1]["upgrades"]) 
        for k, i in ipairs(upgrades) do 
            addVehicleUpgrade( veh, i ) 
        end 
            setElementData (source, "System.Vehicle", veh) 
            outputChatBox("Your car has been spawned!",source, 0, 255, 0) 
        else 
            local nveh = getElementData (source, "System.Vehicle") 
            outputChatBox("Your car has been despawned!", source, 0, 255, 0) 
            local nups = toJSON(getVehicleUpgrades(nveh)) 
            executeSQLQuery("UPDATE vehicles SET upgrades=? WHERE accName=? AND id=?", nups, accName, carID) 
            setElementData (source, "System.Vehicle", false) 
            destroyElement(nveh) 
        end 
    end 
end  
addEvent("spawnDespawnCar", true) 
addEventHandler("spawnDespawnCar", root, spawnDespawnCar) 

Do not yield your back to your enemy, might feel something strange in your ass.

Two things are infinite the universe and human stupidity and i'm not sure about the universe.

UF: IsTextInGridList | GetGridListRowIndexFromText | Table.removeValue | removeHex | dxDrawTriangle

Skype: SaSuki102 | About Me | Youtube channel | Lua Tips & Tricks | Lua Strings | Lua Tables | Lua Operators

Posted (edited)

the code is working fine check your database if there is upgrades column and check if there is a result or not.

Edited by Guest

Do not yield your back to your enemy, might feel something strange in your ass.

Two things are infinite the universe and human stupidity and i'm not sure about the universe.

UF: IsTextInGridList | GetGridListRowIndexFromText | Table.removeValue | removeHex | dxDrawTriangle

Skype: SaSuki102 | About Me | Youtube channel | Lua Tips & Tricks | Lua Strings | Lua Tables | Lua Operators

Posted
executeSQLQuery("INSERT INTO vehUpgrades (accName, id, upgrades)", accName, carID, "empty") 

I'm not sure about the "empty" value, with what should I replace it?

(I wanted to make an independant table for the upgrades, so I've changed it a bit).

Posted

Well, if you still need help.

I'll tell you a BIG MISTAKE YOU DID, A REALLY BIG ONE. SQLite is case sensitive.

  
 local result = executeSQLQuery("SELECT * FROM vehicles WHERE accName=? and ID=?", tostring(accName), tonumber(carID)) 
 executeSQLQuery("UPDATE vehicles SET upgrades=? WHERE accName=? AND id=?", nups, accName, carID) 
  

well if you see this, do you observe id and ID?

Please show me your CREATE query, then I'll tell you the which should solve this problem.

Posted

Yes, I still need help lmao..

function startTables() 
    executeSQLQuery("CREATE TABLE IF NOT EXISTS vehicles (accName TEXT, id NUM, price NUM, health NUM, r NUM, g NUM, b NUM, x NUM, y NUM, z NUM, locked TEXT)") 
    executeSQLQuery("CREATE TABLE IF NOT EXISTS vehUpgrades (accName TEXT, id NUM, upgrades TEXT)") 
end 
addEventHandler("onResourceStart", resourceRoot, startTables) 

Posted

I hope this help you :

You can use a string to save instead of a toJSON tables.

function startTables() 
    executeSQLQuery("CREATE TABLE IF NOT EXISTS vehicles (accName TEXT, id NUM, price NUM, health NUM, r NUM, g NUM, b NUM, x NUM, y NUM, z NUM, locked TEXT, vehUpgrades STRING)") 
addEventHandler("onResourceStart", resourceRoot, startTables) 
  
  
-- updating upgrade :  
local upgrades = getVehicleUpgrades(veh) 
local save = table.concat(upgrades, ";") 
executeSQLQuery("UPDATE vehicles SET vehUpgrades=? WHERE accName=? AND id=?", save, accName, carID) 
  
-- adding upgrades :  
querySQL = executeSQLQuery("SELECT * FROM vehicles WHERE accName=? AND id=?", accName, carID) 
local save = querySQL[1]["vehUpgrades"] 
local upgrades = split(save, string.byte(";")) 
for index, v in pairs(upgrades) do 
        addVehicleUpgrade(veh, v) 
end 
  
  
  

430x73_B2E03D_FF9900_000000_000000.png

Some people want it to happen, some wish it would happen, others make it happen.

Posted
WARNING: [server]\vehicles\server.lua:59: Bad argument @ 'split' [Expected string at argument 1, got nil] 
[2015-06-16 15:15:58] ERROR: [server]\vehicles\server.lua:60: bad argument #1 to 'pairs' (table expected, got number) 

Posted
Yes, I still need help lmao..
function startTables() 
    executeSQLQuery("CREATE TABLE IF NOT EXISTS vehicles (accName TEXT, id NUM, price NUM, health NUM, r NUM, g NUM, b NUM, x NUM, y NUM, z NUM, locked TEXT)") 
    executeSQLQuery("CREATE TABLE IF NOT EXISTS vehUpgrades (accName TEXT, id NUM, upgrades TEXT)") 
end 
addEventHandler("onResourceStart", resourceRoot, startTables) 

Well I got this now!

  
local result = executeSQLQuery("SELECT * FROM vehicles WHERE accName=? and ID=?", tostring(accName), tonumber(carID)) 
  

to

  
local result = executeSQLQuery("SELECT * FROM vehicles WHERE accName=? and id=?", tostring(accName), tonumber(carID)) 
  

Posted
Anubhav that wasn't the issue, Markeloff was right and I made a slight mistake, fixed and works now.

Thanks everyone!

You're welcome, anytime :D

430x73_B2E03D_FF9900_000000_000000.png

Some people want it to happen, some wish it would happen, others make it happen.

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