1

So I am working on a script for GTA5 and I need to transfer data over to a js script. However so I don't need to send multiple arrays to js I require a table, the template for the table should appear as below.

The issue I'm having at the moment is in the second section where I receive all vehicles and loop through each to add it to said 'vehicleTable'. I haven't been able to find the "table.insert" method used in a multilayered table

So far I've tried the following

table.insert(vehicleTable,vehicleTable[class][i][vehicleName])

This seems to store an 'object'(table)? so it does not show up when called in the latter for loop Next,

vehicleTable = vehicleTable + vehicleTable[class][i][vehicleName]

This seemed like it was going nowhere as I either got a error or nothing happened. Next,

table.insert(vehicleTable,class)
table.insert(vehicleTable[class],i)
table.insert(vehicleTable[class][i],vehicleName)

This one failed on the second line, I'm unsure why however it didn't even reach the next problem I saw later which would be the fact that line 3 had no way to specify the "Name" field. Lastly the current one,

local test = {[class] = {[i]={["Name"]=vehicleName}}}
table.insert(vehicleTable,test)

It works without errors but ultimately it doesn't file it in the table instead it seems to create its own branch so object within the object.

And after about 3 hours of zero progress on this topic I turn to the stack overflow for assistance.

local vehicleTable = {
    ["Sports"] = {
        [1] = {["Name"] = "ASS", ["Hash"] = "Asshole2"},
        [2] = {["Name"] = "ASS2", ["Hash"] = "Asshole1"}
    },
    ["Muscle"] = {
        [1] = {["Name"] = "Sedi", ["Hash"] = "Sedina5"}
    },
    ["Compacts"] = {
        [1] = {["Name"] = "MuscleCar", ["Hash"] = "MCar2"}
    },
    ["Sedan"] = {
        [1] = {["Name"] = "Blowthing", ["Hash"] = "Blowthing887"}
    }
}


local vehicles = GetAllVehicleModels();
for i=1, #vehicles do
    local class = vehicleClasses[GetVehicleClassFromName(vehicles[i])]
    local vehicleName = GetLabelText(GetDisplayNameFromVehicleModel(vehicles[i]))
    print(vehicles[i].. " " .. class .. " " .. vehicleName)

    local test = {[class] = {[i]={["Name"]=vehicleName}}}
    table.insert(vehicleTable,test)

end

for k in pairs(vehicleTable) do
    print(k)
    -- for v in pairs(vehicleTable[k]) do
    --     print(v .. " " .. #vehicleTable[k])
    -- end
end

If there is not way to add to a library / table how would I go about sorting all this without needing to send a million (hash, name, etc...) requests to js?

Any recommendations or support would be much appreciated.

1 Answers1

0

Aside the fact that you do not provide the definition of multiple functions and tables used in your code that would be necessary to provide a complete answere without making assumptions there are many misconceptions regarding very basic topics in Lua.

The most prominent is that you don't know how to use table.insert and what it can do. It will insert (append by default) a numeric field to a table. Given that you have non-numeric keys in your vehicleTable this doesn't make too much sense.

You also don't know how to use the + operator and that it does not make any sense to add a table and a string.

Most of your code seems to be the result of guess work and trial and error. Instead of referring to the Lua manual so you know how to use table.insert and how to index tables properly you spend 3 hours trying all kinds of variations of your incorrect code.

Assuming a vehicle model is a table like {["Name"] = "MyCar", ["Hash"] = "MyCarHash"} you can add it to a vehicle class like so:

table.insert(vehicleTable["Sedan"], {["Name"] = "MyCar", ["Hash"] = "MyCarHash"})

This makes sense because vehicleTable.Sedan has numeric indices. And after that line it would contain 2 cars.

Read the manual. Then revisit your code and fix your errors.

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • I appreciate the honesty however I will say, at 3am in the morning after 3 hours of no progress on the topic one becomes a bit frustrated and try's anything that potentially works. Anyways. ` table.insert(vehicleTable[class], {["Name"] = vehicleName, ["Hash"] = vehicles[i]}) ` Doesn't work as there are more than 4 vehicle classes (23) so it replies "bad argument #1 to 'insert' (table expected, got nil)" which is understandable. So assuming I need to add a new insert for the class how would I set that up since it seems a bit more complicated. – Latisimus Firestorm Nov 14 '22 at 10:37
  • Also missing are FiveM Native functions [link](https://docs.fivem.net/natives/) These, GetAllVehicleModels() GetVehicleClassFromName() GetDisplayNameFromVehicleModel() GetLabelText() As for vehicleClasses thats a table that I created to sort through a numeric value and pop out the actual class name since GetVehicleClassFromName() returns 0-22 for the class. – Latisimus Firestorm Nov 14 '22 at 10:39