Jump to content

Vehicles


Recommended Posts

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
Link to comment
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) 

Link to comment
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).

Link to comment

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.

Link to comment

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) 

Link to comment

Have you tried doing:

local upgrades = fromJSON(result[1].upgrades) 

or

local upgrades = fromJSON(result.upgrades) 

instead of what you currently have which is

local upgrades = fromJSON(result[1]["upgrades"]) 

Not sure if it will make any difference/fix the issue, but I've never seen the way you do it before.

Link to comment

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 
  
  
  

Link to comment
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)) 
  

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