1

I am new to lua so sorry if my code may look bad, I have been trying to randomize the x, y, z values in a table without a success. How could I do so?

Here is an error I get:

Error loading script jobs/poolworker.lua in resource esx_jobs: @esx_jobs/jobs/poolworker.lua:156: attempt to index a nil value (global 'Delivery')

Here is my current script:


Config.Jobs.slaughterer = {



Delivery = {
            Pos = {x,y,z},
            
            Color = {r = 50, g = 200, b = 50},
            Size = {x = 5.0, y = 5.0, z = 3.0},
            Marker = 1,
            Blip = true,
            Name = _U('delivery_point'),
            Type = 'delivery',
            Spawner = 1,
            Item = {
                {
                    name = _U('delivery'),
                    time = 0.5,
                    remove = 1,
                    max = 100, -- if not present, probably an error at itemQtty >= item.max in esx_jobs_sv.lua
                    price = 13,
                    requires = 'bottle',
                    requires_name = _U('chl_bottle'),
                    drop = 100
                }
            },
            Hint = _U('p_deliver_button')
        }
        
    }
    
}
test = {x = 162.329666,  y = -194.452744, z = 54.217529}
if delivery == 1 then
    table.insert(Delivery[1]["Pos"], test)
elseif delivery == 2 then
    table.insert(Delivery[1]["Pos"], test)
end ```


  
[1]: https://i.stack.imgur.com/ZIXhJ.png
Thank you, whoever is going to help me.
Nifim
  • 4,758
  • 2
  • 12
  • 31
Bakaguya
  • 29
  • 5

1 Answers1

0

So a couple of things.

For your use case you probably don't want to use table.insert, since the goal of table.insert insert X at the next open numerical index for the table Ex:

local someTable = {}
table.insert(someTable, 123)
table.insert(someTable, "Hello World")
-- someTable is {123, "Hello World"} which is shorthand for {[1] = 123, [2] = "Hello World"}

Instead you probably want to directly index Tables can be index like so:

local someTable = {
    Thing = 1
}
print(someTable["Thing"]) -- print's 1 
someTable["SomethingElse"] = 2
print(someTable["SomethingElse"]) -- print's 2

From what I could gather from the output Delivery doesn't seem to be a variable, rather it's an index within the Config.Jobs.slaughterer table. So maybe try:

test = {x = 162.329666,  y = -194.452744, z = 54.217529}
if delivery == 1 then
    Config.Jobs.slaughterer.Delivery["Pos"] = test
elseif delivery == 2 then
    Config.Jobs.slaughterer.Delivery["Pos"] = test
end
kingerman88
  • 521
  • 3
  • 12