Jump to content

attempt to index field "?" (a nil value)


thund3rbird23

Recommended Posts

    local dim = getElementDimension(localPlayer)
    if dim ~= 0 then
        local px, py, pz = getElementPosition(localPlayer) 
        if not (getDistanceBetweenPoints3D(arrestPoints[dim][1], arrestPoints[dim][2], arrestPoints[dim][3], px, py, pz) <= 7) then -- here is the error
            exports.s_alert:showAlert("error", "Nem vagy letartóztatási hely közelében.")
            return
        end
    end

arrestPoints:

 

arrestPoints = {
    --[Int ID] = {X, Y, Z}
    [10] = {215.72779846191, 115.77359771729, 1000.2130737305},
	[10] = {219.50369262695, 115.5871963501, 1000.0123291016},
	[10] = {223.50689697266, 115.81269836426, 1000.3059082031},
	[10] = {227.45030212402, 115.83280181885, 1000.3964233398},
}

 

Edited by thund3rbird23
Link to comment

Ok, I fixed it by changing the if dim ~= 0 then to the dimension where I am (174).

Now getting this error:

attempt to get length of field '?' (a nil value)

For this:
 

 local rnd = math.random(1, #cells[dim])



The full function code, where is the error is:

function dropPlayerToTheCell(player, time, reason, bail, bailPrice, arrestedBy)
    local arrestText = time .. "/" .. reason .. "/" .. tostring(bail) .. "/" .. bailPrice .. "/" .. arrestedBy
    arrestedPlayers[player] = {
        ["timer"] = setTimer(unjailPlayer, time * 60000, 1, player),
        ["time"] = time,
        ["reason"] = reason,
        ["bail"] = {bail, bailPrice},
        ["arrestedBy"] = arrestedBy,
    }

   local dim = getElementDimension(player)
    if not cells[dim] then
        dim = defaultCells
    end
    local int = getElementInterior(player)
    if int == 0 then
        int = defaultInt
    end
    local rnd = math.random(1, #cells[dim]) -- here is the error

    setElementPosition(player, cells[dim][rnd][1], cells[dim][rnd][2], cells[dim][rnd][3])
    setElementRotation(player, 0, 0, cells[dim][rnd][4])
    setElementDimension(player, dim)
    setElementInterior(player, int)
    setElementData(player, "char.arrested", arrestText)
    removePedFromVehicle(player)
    print(cells[dim][rnd][1] .. " " .. cells[dim][rnd][2] .. " " .. cells[dim][rnd][3] .. " " .. dim .. " " .. int)
end
defaultCells = 174
defaultInt = 10

cells = {
    --[Int ID] = {{Cellák pos}}
    [10] = {215.36199951172, 108.91190338135, 1000.9522705078, 180},
	[10] = {219.84390258789, 108.5353012085, 1000.9569702148, 180},
	[10] = {223.37170410156, 107.92459869385, 1000.8811035156, 180},
	[10] = {227.17649841309, 107.85209655762, 1000.6198120117, 180},
}

 

Edited by thund3rbird23
Link to comment
local rnd = math.random(1, #cells[dim])

"attempt to get length of field '?' (a nil value)" means you've tried to get the length (# operator) of a nil value (cells[dim]), which means there is no value in the cells table with the index corresponding to the value of dim. Your if-check sets the value of dim to the value of defaultCells, 174, but cells[174] also doesn't exist. Only cells[10] exists.


cells = {
  --[Int ID] = {{Cellák pos}}
  [10] = {215.36199951172, 108.91190338135, 1000.9522705078, 180},
  [10] = {219.84390258789, 108.5353012085, 1000.9569702148, 180},
  [10] = {223.37170410156, 107.92459869385, 1000.8811035156, 180},
  [10] = {227.17649841309, 107.85209655762, 1000.6198120117, 180},
}

By the way, you can't have multiple values under one index. The table above, after construction, is equivalent to

cells = {
  [10] = {227.17649841309, 107.85209655762, 1000.6198120117, 180}
}

that is, every line starting with [10] =  overrides the previous line starting with [10] =

You probably wanted something like

cells = {
  --[Int ID] = { [Dim ID] = { {Cellák pos}, {Cellák pos}, ... }, }
  [10] = { -- in interior 10
    [174] = { -- in dimension 174
      {215.36199951172, 108.91190338135, 1000.9522705078, 180}, -- first cell in int 10 dim 174
      {219.84390258789, 108.5353012085, 1000.9569702148, 180}, -- second cell in int 10 dim 174
      {223.37170410156, 107.92459869385, 1000.8811035156, 180}, -- third cell in int 10 dim 174
      {227.17649841309, 107.85209655762, 1000.6198120117, 180}, -- fourth cell in int 10 dim 174
    },
  },
}

-- ...
local int = getElementInterior(player)
if int == 0 or not cells[int] then -- if interior is 0 or there are no cells in the interior
  int = defaultInt -- set int to default (10)
end
local dim = getElementDimension(player)
if not cells[int][dim] then -- if there are no cells in that interior and dimension
  dim = defaultCells -- set dimension to default (174)
end
local rnd = math.random(1, #cells[int][dim]) -- pick a random cell from 1 to however many there are for that interior and dimension

local randomCell = cells[int][dim][rnd] -- index the cells table for the random cell
-- ...

 

Edited by MrTasty
  • Thanks 1
Link to comment

Thank you. So I can reference later to the cells with randomCell[1] , randomCell[2] etc...?

Always pick the second cell {219.84390258789, 108.5353012085, 1000.9569702148, 180}

local randomCell = cells[int][dim][rnd]

    setElementPosition(player, randomCell[1], randomCell[2], randomCell[3])
    setElementRotation(player, 0, 0, randomCell[4])
    setElementDimension(player, dim)
    setElementInterior(player, int)
    setElementData(player, "char.arrested", arrestText)
    removePedFromVehicle(player)

 

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