Jump to content

vehicles table


Chaos

Recommended Posts

Posted

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) 

Posted

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) 
  

Posted

One question if i bought car and another player also bought car it will save same table that i used it to save my vehicles?

  • Moderators
Posted

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?

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted

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 ?

Posted
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) 

Posted

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

Posted

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

- New , Kill System

- New, GameMode Intro

- Leve / Exp System

- New nametag showing style

- New , Hud For Players

- Skin Selection from SA-MP

- Money System / Buy Weapons

- Drop Weapons

- New, Flood System

- New , Group Assign

- Gun license For Weapons

- Random Rule System For Money

Posted
  
 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

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

  • Moderators
Posted

Line 10:

Check the table data before proceed.

local vehicle = carsTable[source] 
if vehicle then 
--... 
end 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

  • Moderators
Posted

I don't have time for paid scripting.

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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