Jump to content

Car Shop


Recommended Posts

1.How can I make it so when the cars explode they wont respawn

2.How can I make it so when I do /respawn it respawns the cars at the peoples houses who bought them

Client

GUIEditor_Window = {} 
GUIEditor_Button = {} 
GUIEditor_Label = {} 
  
localPlayer = getLocalPlayer () 
  
local screenWidth, screenHeight = guiGetScreenSize() 
  
function showCarBuyMenu( price ) 
    showCursor ( true ) 
    GUIEditor_Window[1] = guiCreateWindow(0.3281,0.35,0.2362,0.2778,"This car is for sale",true) 
    GUIEditor_Label[1] = guiCreateLabel(0.0265,0.088,0.9418,0.228,"Price: "..tostring(price).."$",true,GUIEditor_Window[1]) 
    guiLabelSetColor(GUIEditor_Label[1],255,255,255) 
    guiLabelSetVerticalAlign(GUIEditor_Label[1],"top") 
    guiLabelSetHorizontalAlign(GUIEditor_Label[1],"left",false) 
    guiSetFont(GUIEditor_Label[1],"sa-header") 
    GUIEditor_Button[1] = guiCreateButton(0.1905,0.352,0.6243,0.252,"Buy",true,GUIEditor_Window[1]) 
    guiSetFont(GUIEditor_Button[1],"sa-header") 
    GUIEditor_Button[2] = guiCreateButton(0.2831,0.7,0.4101,0.16,"Close",true,GUIEditor_Window[1]) 
    addEventHandler("onClientGUIClick", GUIEditor_Button[1], 
        function () 
            guiSetVisible ( GUIEditor_Window[1], false ) 
            triggerServerEvent ( "acceptBuyCar", getLocalPlayer()) 
            showCursor ( false ) 
            toggleAllControls ( true ) 
        end 
    ,false) 
    addEventHandler("onClientGUIClick", GUIEditor_Button[2], 
        function () 
            guiSetVisible ( GUIEditor_Window[1], false ) 
            toggleAllControls ( true ) 
            setControlState ( "enter_exit", true ) 
            showCursor ( false ) 
        end 
    ,false) 
end 
  
addEvent( "showBuyCar", true ) 
addEventHandler( "showBuyCar", getRootElement(), showCarBuyMenu ) 

Server

cars={} 
buycarpickup={} 
maxcars = 0 
  
addEventHandler ( "onResourceStart", getResourceRootElement(), 
function () 
  local root = xmlLoadFile ("cars.xml") 
  local houseroot = xmlFindChild (root,"cars",0) 
  if (houseroot) then 
    for i,v in ipairs (xmlNodeGetChildren(houseroot)) do 
      local carmodel = xmlNodeGetAttribute (v,"model") 
      local x = xmlNodeGetAttribute (v,"x") 
      local y = xmlNodeGetAttribute (v,"y") 
      local z = xmlNodeGetAttribute (v,"z") 
      local color1 = xmlNodeGetAttribute (v,"color1") 
      local color2 = xmlNodeGetAttribute (v,"color2") 
      local owner = xmlNodeGetAttribute (v,"owner") 
      local price = xmlNodeGetAttribute (v,"price") 
      local lock = xmlNodeGetAttribute (v,"lock") 
      local a = xmlNodeGetAttribute (v,"a") 
      cars[i] = createVehicle ( tonumber(carmodel),tonumber(x),tonumber(y),tonumber(z), 0, 0, tonumber(a) ) 
      setElementInterior ( cars[i], 0 ) 
      setElementData (cars[i],"xpos",tonumber(x)) 
      setElementData (cars[i],"ypos",tonumber(y)) 
      setElementData (cars[i],"zpos",tonumber(z)) 
      setElementData (cars[i],"angle",tonumber(a)) 
      setElementData (cars[i],"owner",owner) 
      setElementData (cars[i],"price",tonumber(price)) 
      setElementData (cars[i],"lock",tonumber(lock)) 
      setElementData (cars[i], "num", i ) 
      setVehicleColor ( cars[i], tonumber(color1), tonumber(color2), 0, 0 ) 
      if(lock == 1) then 
        setVehicleLocked ( car, true ) 
      end 
      if getElementData ( cars[i], "owner" ) == "Nobody" then 
        buycarpickup[i] = createPickup ( x,  y, z, 3, 1274 ) 
        attachElements ( buycarpickup[i], cars[i], 0, 0, 1.9 ) 
      end 
      maxcars = maxcars+1 
    end 
    outputDebugString ("Cars loaded!") 
  end 
end 
) 
  
function saveCars () 
    local root = xmlLoadFile ("cars.xml") 
    local houseroot = xmlFindChild (root,"cars",0) 
        if (houseroot) then 
        for i,v in ipairs (xmlNodeGetChildren(houseroot)) do 
         local color1, color2, color3, color4 = getVehicleColor ( cars[i] ) 
          xmlNodeSetAttribute ( v, "model", getElementModel(cars[i]) ) 
          xmlNodeSetAttribute ( v, "x", getElementData(cars[i], "xpos") ) 
          xmlNodeSetAttribute ( v, "y", getElementData(cars[i], "ypos") ) 
          xmlNodeSetAttribute ( v, "z", getElementData(cars[i], "zpos") ) 
          xmlNodeSetAttribute ( v, "a", getElementData(cars[i], "angle") ) 
          xmlNodeSetAttribute ( v, "color1", color1 ) 
          xmlNodeSetAttribute ( v, "color2", color2 ) 
          xmlNodeSetAttribute ( v, "owner", getElementData(cars[i], "owner") ) 
          xmlNodeSetAttribute ( v, "price", getElementData(cars[i], "price") ) 
          xmlNodeSetAttribute ( v, "lock", getElementData(cars[i], "lock") ) 
        end 
        xmlSaveFile(root) 
    end 
end 
  
function adminCreateVehicle ( source, cmd ) 
    local accName = getAccountName ( getPlayerAccount ( source ) ) 
    if isObjectInACLGroup ( "user." .. accName, aclGetGroup ( "Admin" ) ) then 
      local root = xmlLoadFile ("cars.xml") 
      local houseroot = xmlFindChild (root,"cars",0) 
      local createdcar = xmlCreateChild ( houseroot, "vehicle" ) 
      local carmodel = xmlNodeSetAttribute ( createdcar, "model", "451" ) 
      local x, y, z = getElementPosition ( source ) 
      local xa,ya,a = getElementRotation ( source ) 
      xmlNodeSetAttribute ( createdcar, "x", x ) 
      xmlNodeSetAttribute ( createdcar, "y", y ) 
      xmlNodeSetAttribute ( createdcar, "z", z ) 
      xmlNodeSetAttribute ( createdcar, "a", a ) 
      xmlNodeSetAttribute ( createdcar, "color1", "0" ) 
      xmlNodeSetAttribute ( createdcar, "color2", "0" ) 
      xmlNodeSetAttribute ( createdcar, "owner", "Nobody" ) 
      xmlNodeSetAttribute ( createdcar, "price", "0" ) 
      xmlNodeSetAttribute ( createdcar, "lock", "0" ) 
      cars[maxcars+1] = createVehicle ( 451,x,y,z,0,0,a ) 
      setElementData (cars[maxcars+1],"xpos",x) 
      setElementData (cars[maxcars+1],"ypos",y) 
      setElementData (cars[maxcars+1],"zpos",z) 
      setElementData (cars[maxcars+1],"angle", a) 
      setElementData (cars[maxcars+1],"owner","Nobody") 
      setElementData (cars[maxcars+1],"price",0) 
      setElementData (cars[maxcars+1],"lock",0) 
      xmlSaveFile(root) 
      maxcars = maxcars+1 
    else 
        outputChatBox ("Error: You can't use this command",source, 255,255,127 ) 
    end 
end 
  
addCommandHandler ("acarcreate", adminCreateVehicle) 
addCommandHandler ("acarsave", saveCars) 
  
function getCar ( car ) 
    return cars[car] 
end 
  
function enterVehicle ( source, seat, jacked ) 
    local playercar = getPedOccupiedVehicle ( source ) 
    if(seat == 0) then 
        if (getElementData ( playercar, "owner" )) then 
            if(getElementData ( playercar, "owner" ) ~= "Nobody" ) then 
                if(getElementData ( playercar, "owner" ) == getPlayerName ( source )) then 
                    outputChatBox ("It is your car!",source, 255,255,127 ) 
                else 
                    outputChatBox ("This vehicle owner: "..getElementData ( playercar, "owner" ).."",source, 255,255,127 ) 
                end 
            else 
                local price = getElementData ( playercar, "price" ) 
                triggerClientEvent ( source, "showBuyCar", source, price) 
                toggleAllControls ( source, false, true, false ) 
                return true 
            end 
        end 
    end 
end 
  
addEventHandler ( "onVehicleEnter", getRootElement(), enterVehicle ) 
  
function AcceptToBuyCar ( ) 
    local playercar = getPedOccupiedVehicle ( source ) 
    if(getElementData ( playercar, "owner" ) == "Nobody") then 
        if(getPlayerMoney ( source ) > tonumber(getElementData ( playercar, "price"))) then 
            setElementData ( playercar, "owner", getPlayerName ( source )) 
            takePlayerMoney ( source, tonumber(getElementData ( playercar, "price" ))) 
            toggleAllControls ( source, true ) 
            outputChatBox ("You buy this car!", source, 243,149,72 ) 
            destroyElement(buycarpickup[getElementData(playercar,"num")]) 
            saveCars () 
        else 
            outputChatBox ("Error: You don't have enought money", source, 243,149,72 ) 
            setControlState ( source, "enter_exit", true ) 
        end 
    end 
end 
addEvent("acceptBuyCar",true) 
addEventHandler("acceptBuyCar",root,AcceptToBuyCar) 
  
function playerCarLock ( source, cmd) 
    for i,v in ipairs (cars) do 
        if(getElementData ( cars[i], "owner" ) == getPlayerName ( source )) then 
            setVehicleLocked ( cars[i], true ) 
            setElementData (cars[i],"lock", 1) 
            outputChatBox ("You closed your car",source, 243,149,72 ) 
            saveCars() 
        end 
    end 
end 
  
addCommandHandler ("lockmycar",playerCarLock) 
  
  
function playerCarUnLock ( source, cmd) 
    for i,v in ipairs (cars) do 
        if(getElementData ( cars[i], "owner" ) == getPlayerName ( source )) then 
            setVehicleLocked ( cars[i], false ) 
            setElementData (cars[i],"lock", 0) 
            outputChatBox ("You opened your car",source, 243,149,72 ) 
            saveCars() 
        end 
    end 
end 
  
addCommandHandler ("unlockmycar",playerCarUnLock) 
  

Other

function adminSetCarModel ( source, cmd, model ) 
    if(model) then 
        if((tonumber(model) > 399) and (tonumber(model) < 612))then 
            local accName = getAccountName ( getPlayerAccount ( source ) ) 
            if isObjectInACLGroup ( "user." .. accName, aclGetGroup ( "Admin" ) ) then 
                if(isPedInVehicle(source)) then 
                    local playercar = getPedOccupiedVehicle ( source ) 
                    setElementModel (playercar,tonumber(model)) 
                    saveCars() 
                else 
                    outputChatBox ("Error: You isn't in car",source, 255,255,127 ) 
                end 
            else 
                outputChatBox ("Error: You can't use this command",source, 255,255,127 ) 
            end 
        else 
            outputChatBox ("Error: Model ID must be between 400 and 611",source, 255,255,127 ) 
        end 
    else 
        outputChatBox ("Правильно: /acarmodel vehicle model",source, 255,255,127 ) 
    end 
end 
  
addCommandHandler ("acarmodel", adminSetCarModel) 
  
function adminSetCarColor ( source, cmd, color1, color2 ) 
    if(color2) then 
        local accName = getAccountName ( getPlayerAccount ( source ) ) 
        if isObjectInACLGroup ( "user." .. accName, aclGetGroup ( "Admin" ) ) then 
            if(isPedInVehicle(source)) then 
                local playercar = getPedOccupiedVehicle ( source ) 
                setVehicleColor (playercar,color1,color2,0,0) 
                saveCars() 
            else 
                outputChatBox ("Error: You isn't in car",source, 255,255,127 ) 
            end 
        else 
            outputChatBox ("Error: You can't use this command",source, 255,255,127 ) 
        end 
     else 
        outputChatBox ("Correct: /acarcolor [color 1] color 2",source, 255,255,127 ) 
    end 
end 
  
addCommandHandler ("acarcolor", adminSetCarColor) 
  
function adminSetCarPrice ( source, cmd, carprice ) 
    if(carprice) then 
        local accName = getAccountName ( getPlayerAccount ( source ) ) 
        if isObjectInACLGroup ( "user." .. accName, aclGetGroup ( "Admin" ) ) then 
            if(isPedInVehicle(source)) then 
                local playercar = getPedOccupiedVehicle ( source ) 
                setElementData(playercar, "price", tonumber(carprice)) 
                outputChatBox ("You set car "..tostring(playercar).." price $"..tostring(carprice).."",source, 243,149,72 ) 
                saveCars() 
            else 
                outputChatBox ("Error: You isn't in car",source, 255,255,127 ) 
            end 
        else 
            outputChatBox ("Error: You can't use this command",source, 255,255,127 ) 
        end 
     else 
        outputChatBox ("Correct: /acarprice car price",source, 255,255,127 ) 
    end 
end 
  
addCommandHandler ("acarprice", adminSetCarPrice) 
  
function adminSetCarPark ( source, cmd) 
    local accName = getAccountName ( getPlayerAccount ( source ) ) 
    if isObjectInACLGroup ( "user." .. accName, aclGetGroup ( "Admin" ) ) then 
        if(isPedInVehicle(source)) then 
            local playercar = getPedOccupiedVehicle ( source ) 
            local x, y, z = getElementPosition ( source ) 
            local xa,ya,a = getElementRotation ( source ) 
            setElementData (playercar,"xpos",x) 
            setElementData (playercar,"ypos",y) 
            setElementData (playercar,"zpos",z) 
            setElementData (playercar,"angle",a) 
            outputChatBox ("You set car "..tostring(playercar).." spawn point",source, 243,149,72 ) 
            saveCars() 
        else 
            outputChatBox ("Error: You isn't in car",source, 255,255,127 ) 
        end 
    else 
        outputChatBox ("Error: You can't use this command",source, 255,255,127 ) 
    end 
end 
  
addCommandHandler ("acarpark", adminSetCarPark) 
  
function playerCarPark ( source, cmd) 
    if(isPedInVehicle(source)) then 
        local playercar = getPedOccupiedVehicle ( source ) 
        if(getElementData ( playercar, "owner" ) == getPlayerName ( source )) then 
            local x, y, z = getElementPosition ( source ) 
            local xa,ya,a = getElementRotation ( playercar ) 
            setElementData (playercar,"xpos",x) 
            setElementData (playercar,"ypos",y) 
            setElementData (playercar,"zpos",z) 
            setElementData (playercar,"angle",a) 
            outputChatBox ("You parked your car, don't forget to close it!",source, 243,149,72 ) 
            saveCars() 
        else 
            outputChatBox ("Error: It is not your car!",source, 255,255,127 ) 
        end 
    else 
        outputChatBox ("Error: You isn't in car",source, 255,255,127 ) 
    end 
end 
  
addCommandHandler ("carpark",playerCarPark) 
  
function playerCarSell ( source, cmd) 
    if(isPedInVehicle(source)) then 
        local playercar = getPedOccupiedVehicle ( source ) 
        if(getElementData ( playercar, "owner" ) == getPlayerName ( source )) then 
            setElementData (playercar,"owner","Nobody") 
            givePlayerMoney ( source, getElementData ( playercar, "price" )/100*50 ) 
            outputChatBox ("You sold your car!",source, 243,149,72 ) 
            saveCars() 
        else 
            outputChatBox ("Error: It is not your car!",source, 255,255,127 ) 
        end 
    else 
        outputChatBox ("Error: You isn't in car",source, 255,255,127 ) 
    end 
end 
  
addCommandHandler ("carsell",playerCarSell) 
  

Link to comment

Hello cody. no need to be sad or angry. If nobody is helping you is either because they dont have much time, or they dont know how to help u. Not all of us have a lot of free time :)

I will try to help you, but i also dont really have much time. TO start, i saw your command and you are savin the data in xml file. So tell me, when you open the cars.xml file, do you see the data you wanted to save??

Link to comment

Oka, i had some free time and i read your codes. Apparently when a vehicle is destroyed, it is not respawned. Theres no function for that in this resource, so therefore i dont see why you want to create a function that when a vehicle is destroyed, it should not be respawned ! Unless its the contrary that you want. If you want that the vehicle is respawn, then yes, this will require a function to do that.

And for your second question: 2.How can I make it so when I do /respawn it respawns the cars at the peoples houses who bought them.

Here, you mean all the cars or a specific vehicle?? I asked this question because what you are asking here is something that can easily be done. All you need to do is firstly changed the masked or unnamed function that triggers when the resource is started to a name function. Serverside, line 5 to 44. Rewrite it like this :

function spawnVehicles() 
  local root = xmlLoadFile ("cars.xml") 
  local houseroot = xmlFindChild (root,"cars",0) 
  if (houseroot) then 
    for i,v in ipairs (xmlNodeGetChildren(houseroot)) do 
      local carmodel = xmlNodeGetAttribute (v,"model") 
      local x = xmlNodeGetAttribute (v,"x") 
      local y = xmlNodeGetAttribute (v,"y") 
      local z = xmlNodeGetAttribute (v,"z") 
      local color1 = xmlNodeGetAttribute (v,"color1") 
      local color2 = xmlNodeGetAttribute (v,"color2") 
      local owner = xmlNodeGetAttribute (v,"owner") 
      local price = xmlNodeGetAttribute (v,"price") 
      local lock = xmlNodeGetAttribute (v,"lock") 
      local a = xmlNodeGetAttribute (v,"a") 
      cars[i] = createVehicle ( tonumber(carmodel),tonumber(x),tonumber(y),tonumber(z), 0, 0, tonumber(a) ) 
      setElementInterior ( cars[i], 0 ) 
      setElementData (cars[i],"xpos",tonumber(x)) 
      setElementData (cars[i],"ypos",tonumber(y)) 
      setElementData (cars[i],"zpos",tonumber(z)) 
      setElementData (cars[i],"angle",tonumber(a)) 
      setElementData (cars[i],"owner",owner) 
      setElementData (cars[i],"price",tonumber(price)) 
      setElementData (cars[i],"lock",tonumber(lock)) 
      setElementData (cars[i], "num", i ) 
      setVehicleColor ( cars[i], tonumber(color1), tonumber(color2), 0, 0 ) 
      if(lock == 1) then 
        setVehicleLocked ( car, true ) 
      end 
      if getElementData ( cars[i], "owner" ) == "Nobody" then 
        buycarpickup[i] = createPickup ( x,  y, z, 3, 1274 ) 
        attachElements ( buycarpickup[i], cars[i], 0, 0, 1.9 ) 
      end 
      maxcars = maxcars+1 
    end 
    outputDebugString ("Cars loaded!") 
  end 
end 
addEventHandler ( "onResourceStart", getResourceRootElement(),spawnVehicles) 
  

All i did was changing the masked function to a named function. Now, if you want to respawn all the vehicle when you type the command /respawn , all you will have to do is create a function that will call the spawnVehicle function.. and all this should happen when you type in the command. Here it is :

function respawnVehicle(thePlayer,command) 
spawnVehicles() 
end 
addCommandHandler("respawn",respawnVehicle) 

When you type the /respawn command, it will trigger the respawnVehicle function, which in turn will call the spawnVehicles function to recreate all the vehicles. However, if im not wrong, there will be one issue. When you will type this command, the vehicles that were created when the resource had started will not be destroyed, and will be at the same place. This means when you will type the command /respawn, there will be more probably 2 vehicles with the same location coordinates, same owner, price ..... etc.. If you want to fix this, you will have to destroy all the vehicles that were created before by this resource so that there should not be 2 vehicles of the same data. This can easily be done by a loop.

Also, if something is not working, type /debugscript 3 in the chat, and see if there are any errors that appears below on your screen. If yes, post them here.

I hoped i helped you the way you wanted. And next time, dont be sad or angry if nobody is helping you, because time is something that most of us dont really have much. All you need is to wait. 8) Have a nice day!

Link to comment

Well, i modified the code and it works now. I tested it and it works great. When a vehicle is destroyed, if it doesnt have any owner, the vehicle is respawn in 5 seconds, but if it has an owner, it wont be respawn. To respawn it, only the owner can type /respawn for the vehicle to be respawned. Also, for you to use the /acarcreate command, bear in mind that you need to be admin to do that. Also the /acarpark works just fine. Heres the codes:

severside:

local cars={} 
local buycarpickup={}
local maxcars = 0
local destroyedV = {}
 
function spawnVehicles()
  local root = xmlLoadFile ("cars.xml")
  if not root then
    root = xmlCreateFile("cars.xml","root")
    local cars = xmlCreateChild(root,"car")
    xmlSaveFile(root)
  end
  local houseroot = xmlFindChild (root,"car",0)
  if (houseroot) then
    for i,v in ipairs (xmlNodeGetChildren(houseroot)) do
      local carmodel = xmlNodeGetAttribute (v,"model")
      local x = xmlNodeGetAttribute (v,"x")
      local y = xmlNodeGetAttribute (v,"y")
      local z = xmlNodeGetAttribute (v,"z")
      local color1 = xmlNodeGetAttribute (v,"color1")
      local color2 = xmlNodeGetAttribute (v,"color2")
      local owner = xmlNodeGetAttribute (v,"owner")
      local price = xmlNodeGetAttribute (v,"price")
      local lock = xmlNodeGetAttribute (v,"lock")
      local a = xmlNodeGetAttribute (v,"a")
      cars[i] = createVehicle ( tonumber(carmodel),tonumber(x),tonumber(y),tonumber(z), 0, 0, tonumber(a) )
      setElementInterior ( cars[i], 0 )
      setElementData (cars[i],"xpos",tonumber(x))
      setElementData (cars[i],"ypos",tonumber(y))
      setElementData (cars[i],"zpos",tonumber(z))
      setElementData (cars[i],"angle",tonumber(a))
      setElementData (cars[i],"owner",owner)
      setElementData (cars[i],"price",tonumber(price))
      setElementData (cars[i],"lock",tonumber(lock))
      setElementData (cars[i], "num", i )
      setVehicleColor ( cars[i], tonumber(color1), tonumber(color2), 0, 0 )
      if(lock == 1) then
        setVehicleLocked ( car, true )
      end
      if getElementData ( cars[i], "owner" ) == "Nobody" then
        buycarpickup[i] = createPickup ( x,  y, z, 3, 1274 )
        attachElements ( buycarpickup[i], cars[i], 0, 0, 1.9 )
      end
      maxcars = maxcars+1
    end
    outputDebugString ("Cars loaded!")
  end
end
addEventHandler ( "onResourceStart", getResourceRootElement(),spawnVehicles)
 
function respawnVehicle(thePlayer,command)
    local name = getPlayerName(thePlayer)
    if destroyedV[name] ~= nil then
        local model = destroyedV[name]["model"]
        local x = destroyedV[name]["x"]
        local y = destroyedV[name]["y"]
        local z = destroyedV[name]["z"]
        local a = destroyedV[name]["a"]
        local owner = destroyedV[name]["owner"]
        local price = destroyedV[name]["price"]
        local lock = destroyedV[name]["lock"]
        local num = destroyedV[name]["num"]
        local col1 = destroyedV[name]["col1"]
        local col2 = destroyedV[name]["col2"]
        local col3 = destroyedV[name]["col3"]
        local col4 = destroyedV[name]["model"]
        cars[num] = createVehicle ( tonumber(model),tonumber(x),tonumber(y),tonumber(z), 0, 0, tonumber(a) )
        setElementInterior (cars[num], 0 )
        setElementData (cars[num],"xpos",tonumber(x))
        setElementData (cars[num],"ypos",tonumber(y))
        setElementData (cars[num],"zpos",tonumber(z))
        setElementData (cars[num],"angle",tonumber(a))
        setElementData (cars[num],"owner",owner)
        setElementData (cars[num],"price",tonumber(price))
        setElementData (cars[num],"lock",tonumber(lock))
        setElementData (cars[num], "num",num)
        setVehicleColor (cars[num], tonumber(col1), tonumber(col2), tonumber(col3), tonumber(col4))    
        destroyedV[name] = nil
    end
end
addCommandHandler("respawn",respawnVehicle)
 
 
function saveCars ()
--outputChatBox("okaa triggered !!")
    local root = xmlLoadFile ("cars.xml")
    local houseroot = xmlFindChild (root,"car",0)
        if (houseroot) then
        for i,v in ipairs (xmlNodeGetChildren(houseroot)) do
         local color1, color2, color3, color4 = getVehicleColor ( cars[i] )
          xmlNodeSetAttribute ( v, "model", getElementModel(cars[i]) )
          xmlNodeSetAttribute ( v, "x", getElementData(cars[i], "xpos") )
          xmlNodeSetAttribute ( v, "y", getElementData(cars[i], "ypos") )
          xmlNodeSetAttribute ( v, "z", getElementData(cars[i], "zpos") )
          xmlNodeSetAttribute ( v, "a", getElementData(cars[i], "angle") )
          xmlNodeSetAttribute ( v, "color1", color1 )
          xmlNodeSetAttribute ( v, "color2", color2 )
          xmlNodeSetAttribute ( v, "owner", getElementData(cars[i], "owner") )
          xmlNodeSetAttribute ( v, "price", getElementData(cars[i], "price") )
          xmlNodeSetAttribute ( v, "lock", getElementData(cars[i], "lock") )
        end
        xmlSaveFile(root)
    end
end
 
function adminCreateVehicle ( source, cmd )
    local accName = getAccountName ( getPlayerAccount ( source ) )
    if isObjectInACLGroup ( "user." .. accName, aclGetGroup ( "Admin" ) ) then
      local root = xmlLoadFile ("cars.xml")
      local houseroot = xmlFindChild (root,"car",0)
      local createdcar = xmlCreateChild ( houseroot, "vehicle" )
      local carmodel = xmlNodeSetAttribute ( createdcar, "model", "451" )
      local x, y, z = getElementPosition ( source )
      local xa,ya,a = getElementRotation ( source )
      xmlNodeSetAttribute ( createdcar, "x", x )
      xmlNodeSetAttribute ( createdcar, "y", y )
      xmlNodeSetAttribute ( createdcar, "z", z )
      xmlNodeSetAttribute ( createdcar, "a", a )
      xmlNodeSetAttribute ( createdcar, "color1", "0" )
      xmlNodeSetAttribute ( createdcar, "color2", "0" )
      xmlNodeSetAttribute ( createdcar, "owner", "Nobody" )
      xmlNodeSetAttribute ( createdcar, "price", "0" )
      xmlNodeSetAttribute ( createdcar, "lock", "0" )
      cars[maxcars+1] = createVehicle ( 451,x,y,z,0,0,a )
      buycarpickup[maxcars+1] = createPickup ( x,  y, z, 3, 1274 )
      attachElements ( buycarpickup[maxcars+1], cars[maxcars+1], 0, 0, 1.9 )     
      setElementData (cars[maxcars+1],"xpos",x)
      setElementData (cars[maxcars+1],"ypos",y)
      setElementData (cars[maxcars+1],"zpos",z)
      setElementData (cars[maxcars+1],"angle", a)
      setElementData (cars[maxcars+1],"owner","Nobody")
      setElementData (cars[maxcars+1],"price",0)
      setElementData (cars[maxcars+1],"lock",0)
      setElementData(cars[maxcars+1],"num",maxcars+1)
      xmlSaveFile(root)
      maxcars = maxcars+1
 
    else
        outputChatBox ("Error: You can't use this command",source, 255,255,127 )
    end
end
 
addCommandHandler ("acarcreate", adminCreateVehicle)
addCommandHandler ("acarsave", saveCars)
 
function getCar ( car )
    return cars[car]
end
 
function enterVehicle ( source, seat, jacked )
    local playercar = getPedOccupiedVehicle ( source )
    if(seat == 0) then
        if (getElementData ( playercar, "owner" )) then
            if(getElementData ( playercar, "owner" ) ~= "Nobody" ) then
                if(getElementData ( playercar, "owner" ) == getPlayerName ( source )) then
                    outputChatBox ("It is your car!",source, 255,255,127 )
                else
                    outputChatBox ("This vehicle owner: "..getElementData ( playercar, "owner" ).."",source, 255,255,127 )
                end
            else
                local price = getElementData ( playercar, "price" )
                triggerClientEvent ( source, "showBuyCar", source, price)
                toggleAllControls ( source, false, true, false )
                return true
            end
        end
    end
end
 
addEventHandler ( "onVehicleEnter", getRootElement(), enterVehicle )
 
function AcceptToBuyCar ( )
    local playercar = getPedOccupiedVehicle ( source )
    if(getElementData ( playercar, "owner" ) == "Nobody") then
        if(getPlayerMoney ( source ) > tonumber(getElementData ( playercar, "price"))) then
            setElementData ( playercar, "owner", getPlayerName ( source ))
            takePlayerMoney ( source, tonumber(getElementData ( playercar, "price" )))
            toggleAllControls ( source, true )
            outputChatBox ("You buy this car!", source, 243,149,72 )
            destroyElement(buycarpickup[getElementData(playercar,"num")])
            saveCars ()
        else
            outputChatBox ("Error: You don't have enought money", source, 243,149,72 )
            setControlState ( source, "enter_exit", true )
        end
    end
end
addEvent("acceptBuyCar",true)
addEventHandler("acceptBuyCar",root,AcceptToBuyCar)
 
function playerCarLock ( source, cmd)
    for i,v in ipairs (cars) do
        if(getElementData ( cars[i], "owner" ) == getPlayerName ( source )) then
            setVehicleLocked ( cars[i], true )
            setElementData (cars[i],"lock", 1)
            outputChatBox ("You closed your car",source, 243,149,72 )
            saveCars()
        end
    end
end
 
addCommandHandler ("lockmycar",playerCarLock)
 
 
function playerCarUnLock ( source, cmd)
    for i,v in ipairs (cars) do
        if(getElementData ( cars[i], "owner" ) == getPlayerName ( source )) then
            setVehicleLocked ( cars[i], false )
            setElementData (cars[i],"lock", 0)
            outputChatBox ("You opened your car",source, 243,149,72 )
            saveCars()
        end
    end
end
 
addCommandHandler ("unlockmycar",playerCarUnLock)
 
addEventHandler("onVehicleExplode",getRootElement(),
function ()
    local x = getElementData (source,"xpos")
    local y = getElementData (source,"ypos")
    local z = getElementData (source,"zpos")
    local a = getElementData (source,"angle")
    local owner = getElementData (source,"owner")
    local price = getElementData (source,"price")
    local lock = getElementData (source,"lock")
    local num = getElementData(source,"num")
    local model = getElementModel(source)
    local col1, col2, col3, col4 = getVehicleColor(source)
    destroyElement(source)
    destroyElement(buycarpickup[num])
    if num then
        if owner == "Nobody" then
            setTimer(function()
              cars[num] = createVehicle ( tonumber(model),tonumber(x),tonumber(y),tonumber(z), 0, 0, tonumber(a) )
              setElementInterior (cars[num], 0 )
              setElementData (cars[num],"xpos",tonumber(x))
              setElementData (cars[num],"ypos",tonumber(y))
              setElementData (cars[num],"zpos",tonumber(z))
              setElementData (cars[num],"angle",tonumber(a))
              setElementData (cars[num],"owner",owner)
              setElementData (cars[num],"price",tonumber(price))
              setElementData (cars[num],"lock",tonumber(lock))
              setElementData (cars[num], "num",num)
              buycarpickup[num] = createPickup ( x,  y, z, 3, 1274 )
              attachElements ( buycarpickup[num], cars[num], 0, 0, 1.9 )
              setVehicleColor (cars[num], tonumber(col1), tonumber(col2), tonumber(col3), tonumber(col4))
              end,5000,1)
        else
            destroyedV[owner] = {x = x, y = y, z = z, a = a, owner = owner, price = price, lock = lock, num = num, model = model, col1 = col1, col2 = col2, col3 = col3, col4 = col4}
        end
   
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...