Jump to content

Table help


Recommended Posts

Okay so I've been working on a teleport GUI and it works fine, but I'm at the stage of adding teleports to a table whilst in-game. For some reason, it's not working.

My table:

local data = {  --                               X              Y           Z        Int  Dim  Rot 
[1] = {"1", "Los Santos", "Pershing Square", 1479.9873046875, -1710.9453125, 13.36874961853,    0,  0,  0}, 
[2] = {"2", "San Fierro", "Doherty", -1988.5693359375, 507.0029296875, 35.171875,   0,  0,  90}, 
[3] = {"3", "Las Venturas", "Las Venturas Airport", 1691.6801757813,    1449.1293945313,    10.765375,  0,  0,  268}, 
} 

The function I made which adds a teleport to this table:

addEvent ("addTeleport",true) 
addEventHandler ("addTeleport",root, 
function (tpname, tpdesc) 
local x, y, z = getElementPosition(source) 
local rx, ry, rz = getElementRotation(source) -- fetch all the necessary info for the teleport 
local int = getElementInterior(source) 
local dim = getElementDimension(source) 
local nextindex = findNextTeleportID() 
local nextindexs = tostring(nextindex) 
table.insert(data, [nextindex] = {nextindexs, tpname, tpdesc, x, y, z, int, dim, rz}) 
end) 

The function which finds the next available ID in the table:

function findNextTeleportID() 
    for index, val in ipairs (data) do 
        local finalnum = 0 
        finalnum + 1 
        end 
    return finalnum + 1 
end 

Any help is appreciated, thanks.

Link to comment
function findNextTeleportID() 
    for index, val in ipairs (data) do 
        local finalnum = 0 -- creates a finalnum variable each time the loop runs and sets it to 0 
        finalnum + 1 
        end 
    return finalnum + 1 
end 

This part is wrong and its not even needed. You can just get the length ( or the size if you like ) the table and add +1, like:

local nextIndex = #data + 1 

Link to comment

Also the loop isn't used so no need this part

function findNextTeleportID() 
    for index, val in ipairs (data) do 
        local finalnum = 0 -- creates a finalnum variable each time the loop runs and sets it to 0 
        finalnum + 1 
        end 
    return finalnum + 1 
end 

Link to comment
addEvent("addTeleport",true) 
addEventHandler("addTeleport", root, 
function(tpname, tpdesc) 
    local x, y, z = getElementPosition(source) 
    local rx, ry, rz = getElementRotation(source) -- fetch all the necessary info for the teleport 
    local int = getElementInterior(source) 
    local dim = getElementDimension(source) 
    table.insert(data, {tostring(#data+1), tpname, tpdesc, x, y, z, int, dim, rz}) 
end) 

findNextTeleportID not needed.

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