Jump to content

[Help] Making Car system


Centauro

Recommended Posts

Hello there, I am trying to make an Car system and I face a big trouble. Basically the problem is that I want the resource to be capable to save the last position of any car because when I restart the resource they disappear.

addEventHandler("onResourceStart", resourceRoot, 
function() 
    db = dbConnect("sqlite", "database.db") 
    dbExec(db, "CREATE TABLE IF NOT EXISTS VehicleList (ID, Account, Model, X, Y, Z, RotZ, Colors, Upgrades, Paintjob, Cost, HP)") 
end) 
   z 
    addEventHandler("onResourceStop", resourceRoot, 
        function ( ) 
            if isElement(theVehicle) then 
        local Owner = getElementData(theVehicle, "Owner") 
        if Owner then 
            for index, value in ipairs ( getElementsByType ( "vehicle", Owner ) ) do 
            local x, y, z = getElementPosition(theVehicle) 
            local _, _, rz = getElementRotation(theVehicle) 
            local r1, g1, b1, r2, g2, b2 = getVehicleColor(theVehicle, true) 
            local color = r1..","..g1..","..b1..","..r2..","..g2..","..b2 
            upgrade = "" 
            for _, upgradee in ipairs (getVehicleUpgrades(theVehicle)) do 
                if upgrade == "" then 
                    upgrade = upgradee 
                else 
                    upgrade = upgrade..","..upgradee 
                end 
            end 
            local Paintjob = getVehiclePaintjob(theVehicle) or 3 
            local id = getElementData(theVehicle, "ID") 
            dbExec(db, "UPDATE VehicleList SET X = ?, Y = ?, Z = ?, RotZ = ?, HP = ?, Colors = ?, Upgrades = ?, Paintjob = ? WHERE Account = ? AND ID = ?", x, y, z, rz, getElementHealth(theVehicle), color, upgrade, Paintjob, getAccountName(getPlayerAccount(Owner)), id) 
            end 
        end 
        end 
        end 
    ) 
    
    addEventHandler("onResourceStart", resourceRoot, 
        function(id) 
        if getVehicleByID(id) then 
        else 
   local data = dbPoll(dbQuery(db, "SELECT * FROM VehicleList WHERE Account = ? AND ID = ?", getAccountName(getPlayerAccount(source)), id), -1) 
            local color = split(data[1]["Colors"], ',') 
            r1 = color[1] or 255 
            g1 = color[2] or 255 
            b1 = color[3] or 255 
            r2 = color[4] or 255 
            g2 = color[5] or 255 
            b2 = color[6] or 255 
            vehicle = createVehicle(data[1]["Model"], data[1]["X"], data[1]["Y"], data[1]["Z"], 0, 0, data[1]["RotZ"]) 
            setElementData(vehicle, "ID", id) 
            local upd = split(tostring(data[1]["Upgrades"]), ',') 
            for i, upgrade in ipairs(upd) do 
                addVehicleUpgrade(vehicle, upgrade) 
            end 
            local Paintjob = data[1]["Paintjob"] or 3 
            setVehiclePaintjob(vehicle, Paintjob)  
            setVehicleColor(vehicle, r1, g1, b1, r2, g2, b2) 
            if data[1]["HP"] <= 255.5 then data[1]["HP"] = 255 end 
            setElementHealth(vehicle, data[1]["HP"]) 
            setElementData(vehicle, "Owner", source) 
            setElementData(source, "EsteCarro", vehicle) 
            vv[vehicle] = setTimer(function(source) 
                if not isElement(source) then killTimer(vv[source]) vv[source] = nil end 
                if isElement(source) and getElementHealth(source) <= 255 then 
                    setElementHealth(source, 255.5) 
                    setVehicleDamageProof(source, true) 
                    setVehicleEngineState(source, false) 
                end 
            end, 50, 0, vehicle) 
            addEventHandler("onVehicleDamage", vehicle, 
            function(loss) 
                local account = getAccountName(getPlayerAccount(getElementData(source, "Owner"))) 
                setTimer(function(source) if isElement(source) then dbExec(db, "UPDATE VehicleList SET HP = ? WHERE Account = ? AND Model = ?", getElementHealth(source), account, getElementModel(source)) updateVehicleInfo(getElementData(source, "Owner")) end end, 100, 1, source) 
            end) 
            addEventHandler("onVehicleEnter", vehicle, 
            function(player) 
                if getElementHealth(source) <= 255.5 then  
                    setVehicleEngineState(source, false) 
                else 
                    if isVehicleDamageProof(source) then 
                        setVehicleDamageProof(source, false) 
                    end 
                end 
            end 
            ) 
            end 
            end 
    ) 

Link to comment

You've to base your script to Mysql database so it will automaticly save your car lists and owners

when they loggout, and just update it in the database.

Install LAMP,XAMPP, Apache and MySQL come installed with it.

If you didn't know how to do that, search on forums for some tutorials about how to use XAMPP.

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