Jump to content

vehicles table


Chaos

Recommended Posts

hey,

i have a problem on vehicle shop when i try to buy many cars it will saves but when i restart the resource and buy new car all saved cars will be gone any help ?

Server:

  
carsTable = {} 
  
addEvent ("onBuyVehicle", true) 
addEventHandler ("onBuyVehicle", getRootElement(),  
function(id, cost, name) 
  
  if (getPlayerMoney (source) >= tonumber(cost)) then 
  table.insert ( carsTable, id ) 
  setAccountData (getPlayerAccount(source), "saveCVehicle",toJSON (carsTable)) 
    outputChatBox ("Press F3 To Spawn your Vehicle.", source, 0, 255, 0, true) 
    takePlayerMoney (source, tonumber (cost)) 
  
  else 
    outputChatBox ("You don't have enough money!", source, 255, 0, 0, false) 
  end 
end) 

Link to comment

ok i did it

function onJoin() 
local data = getAccountData (getPlayerAccount(source), "saveCVehicle") 
    if data then 
    table.insert ( carsTable, data ) 
end 
end 
addEventHandler ("onPlayerLogin", getRootElement(),onJoin) 

but i got

un sorted table like that

[ [ "[ [ 471, 471, 534, 582, 543 ] ]", 462 ] ]

  
function test (player) 
    local data = getAccountData (getPlayerAccount(player), "saveCVehicle") 
    if data then 
    outputChatBox (data, player) 
      
    end 
  
end 
addCommandHandler("getinfo",test) 
  

Link to comment
  • Moderators

no it won't.

Vehicles are elements and they will be gone after the resource has restarted.

You need to get all the vehicle data and save that. Not the vehicle it self.

Example: 
getElementPosition 
getElementRotation 
getElementModel 
getElementHealth 
getVehicleWheelStates 
getVehicleColor 
etc. etc. 
  

You really think it was that easy?

Link to comment

i get tired of sorting table

Server :

  
addEvent ("carShopCarBuy", true) 
addEventHandler ("carShopCarBuy", getRootElement(),  
function(id, cost, name) 
  
  if (getPlayerMoney (source) >= tonumber(cost)) then 
  table.insert ( carsTable, id ) 
  setAccountData (getPlayerAccount(source), "cu",toJSON (carsTable)) 
  
    outputChatBox ("Press F3 To Spawn your Vehicle.", source, 0, 255, 0, true) 
    takePlayerMoney (source, tonumber (cost)) 
  
  else 
    outputChatBox ("You don't have enough money!", source, 255, 0, 0, false) 
  end 
end) 
  
  
function onJoin(player) 
local data = getAccountData (getPlayerAccount(player), "cu") 
    if data then 
    local info = fromJSON ( data ) 
    table.insert ( carsTable, info ) 
    triggerClientEvent(player,"movetable",player,info ) 
end 
end 
addCommandHandler("getinfo",onJoin) 
  
  

Client:

addEvent ("movetable", true) 
function shownamesss (info ) 
for i,v in ipairs (info ) do 
    local carName1 = getVehicleNameFromModel (v) 
   -- local row = guiGridListAddRow (carGridList1) 
   -- guiGridListSetItemText (carGridList1, row, 1, carName1, false, true) 
    outputChatBox(carName1) 
  
    end 
end 
  
addEventHandler ("movetable", getRootElement(), shownamesss) 
--------------------------------------------- 

the problem is when i restart resource and buy new car all saved cars will gone how to save table and load it after resource restarted ?

Link to comment
function onJoin() 
local data = getAccountData (getPlayerAccount(source), "saveCVehicle") 
    if data then 
    table.insert ( carsTable, data ) 
end 
end 
addEventHandler ("onPlayerLogin", getRootElement(),onJoin) 

function onJoin(lastAccount, currentAccount) 
local data = getAccountData(currentAccount, "cu") 
    if data then 
    data = fromJSON(data) 
    --[[THIS IS YOUR VEHICLE TABLE]] 
    vehicleTable = data 
end 
end 
addEventHandler ("onPlayerLogin", getRootElement(),onJoin) 

Link to comment

server:

    function onLogin(lastAccount, currentAccount) 
    local data = getAccountData(currentAccount, "cu") 
        if data then 
        data = fromJSON(data) 
        --[[THIS IS YOUR VEHICLE TABLE]] 
        carsTable = data 
        triggerClientEvent(source,"movetable",source,data) 
    end 
    end 
    addEventHandler ("onPlayerLogin", getRootElement(),onLogin) 

client:

------------------------------------------- 
addEvent ("movetable", true) 
function shownamesss (data) 
for i,v in ipairs (data) do 
    local carName1 = getVehicleNameFromModel (v) 
   -- local row = guiGridListAddRow (carGridList1) 
   -- guiGridListSetItemText (carGridList1, row, 1, carName1, false, true) 
    outputChatBox(carName1) 
  
    end 
end 
  
addEventHandler ("movetable", getRootElement(), shownamesss) 
--------------------------------------------- 

vehicles again gone

Link to comment

- try this ,

---Server

addEventHandler ("onPlayerLogin",root, 
function ( ) 
  local Account = getPlayerAccount(source)  
      if (Account) then  
      local nData = getAccountData ( Account, 'cu' ) 
      if ( nData ) then 
      nData = fromJSON(nData ) 
      carsTabel = nData 
      triggerClientEvent ( source, 'movetabel', source, nData ) 
     end 
   end 
end 
) 

--Client

addEvent ('movetable', true) 
addEventHandler ( 'movetable', root, 
function ( nData ) 
for _,v in ipairs (nData) do 
   local carName1 = getVehicleNameFromModel ( v ) 
    local row = guiGridListAddRow (carGridList1) 
    guiGridListSetItemText (carGridList1, row, 1, carName1, false, true) 
    outputChatBox(tostring(carName1)) 
  end 
end 
) 
Link to comment
  
 carsTable = {} 
  
addEvent ("carShopCarBuy", true) 
addEventHandler ("carShopCarBuy", getRootElement(),  
function(id, cost, name) 
  
  if (getPlayerMoney (source) >= tonumber(cost)) then 
  table.insert ( carsTable, id ) 
  setAccountData (getPlayerAccount(source), "cu",toJSON (carsTable)) 
    outputChatBox ("Press F3 To Spawn your Vehicle.", source, 0, 255, 0, true) 
    takePlayerMoney (source, tonumber (cost)) 
  
  else 
    outputChatBox ("You don't have enough money!", source, 255, 0, 0, false) 
  end 
end) 
  
  
     addEventHandler ("onPlayerLogin",root, 
    function ( ) 
      local Account = getPlayerAccount(source) 
          if (Account) then 
          local nData = getAccountData ( Account, 'cu' ) 
          if ( nData ) then 
          nData = fromJSON(nData ) 
          carsTable = nData 
          triggerClientEvent ( source, 'movetable', source, nData ) 
         end 
       end 
    end 
    ) 
     

same and this is the save script

and you can see here when i restart the resource and buy vehicle logout and login

82LCFd.jpg

Link to comment
  
  
 carsTable = {} 
  
addEvent ("carShopCarBuy", true) 
addEventHandler ("carShopCarBuy", getRootElement(), 
function(id, cost, name) 
  
  if (getPlayerMoney (source) >= tonumber(cost)) then 
  table.insert ( carsTable[source], id ) 
  setAccountData (getPlayerAccount(source), "cu",toJSON (carsTable[source])) 
    outputChatBox ("Press F3 To Spawn your Vehicle.", source, 0, 255, 0, true) 
    takePlayerMoney (source, tonumber (cost)) 
  
  else 
    outputChatBox ("You don't have enough money!", source, 255, 0, 0, false) 
  end 
end) 
  
  
     addEventHandler ("onPlayerLogin",root, 
    function ( ) 
      local Account = getPlayerAccount(source) 
          if (Account) then 
          local nData = getAccountData ( Account, 'cu' ) 
          if ( nData ) then 
          nData = fromJSON(nData ) 
          carsTable = nData 
          triggerClientEvent ( source, 'movetable', source, nData ) 
         end 
       end 
    end 
    ) 
    
  

  
addEvent ('movetable', true) 
addEventHandler ( 'movetable', root, 
function ( nData ) 
for _,v in ipairs (nData) do 
   local carName1 = getVehicleNameFromModel ( v ) 
    local row = guiGridListAddRow (carGridList1) 
    guiGridListSetItemText (carGridList1, row, 1, tostring(carName1), false, true) 
    outputChatBox(tostring(carName1)) 
  end 
end 
) 
  

You've to save it for source too.

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