Jump to content

[Solved] Table inside of a table


ManeXi

Recommended Posts

I want to add stuff to a table created inside of a table.

local playerTable = {} 
  
--Some function over here... 
playerTable[number] = {} --Used this to add the table inside of the table (succesfully added) 
playerTable.number[player] = true --I don't know how to add with the index "player" the "true" value in the "playerTable[number]" table 

The error that I get is "attempt to index field 'number' (a nil value)"

Edited by Guest
Link to comment
To add to it:
table.insert(playerTable[1], content) 

That is wrong. You must use

table.insert( table, index, value ) 

From Lua 5.1 Reference Manual

table.insert (table, [pos,] value) 

Yo can make an idea with that, i think...

local table = { } 
  
addCommandHandler( "addtotable", 
    function( player, cmd, index, value ) 
        local index = tonumber( value ) 
        local value = tostring( value ) 
        if value and index then 
            if table[ player ][ index ] == nil then 
                table[ player ][ index ] = value 
            else 
                local value_in_index = table[ player ][ index ].value 
                outputChatBox( "Table index isnt empty" ) 
                outputChatBox( "Value: "..tostring( value_in_index ) ) 
            end 
        end 
    end 
) 

Link to comment

You get that error because number is an undefined variable, and thus equals to nil. Now, you can do table.test with alphabetical keys only - you have to do table[123] for numbers.

  
local table = { 
    test = 'a', 
    ['something'] = 'b', 
    [1] = 'c', 
    ['test2'] = 'd', 
} 
print(table.test) -- a 
print(table.something) -- b 
print(table[1]) -- c 
print(table['test2']) -- d 
---------------------------- 
local players = {} 
local player = getRandomPlayer() 
players[player] = { 
    name = getPlayerName(player), 
} 
print(players[player].name) 

Link to comment
I think he wants to make a dictionary, not a list, If that's the case:
playerTable[number][player] = true 

is the way to go.

Yeah, that seems to work, I'm not sure if "[player] = true" it's inside of the table called as playerTable[number], but seems to work.

And yes the variable "number" is defined, If not I wouldn't be able to create the second table ("playerTable[number] = {}")

Thanks to everyone.

Link to comment

Sorry for taking the show here but you can also do this:

  
playerTable = { 
    number = {}, 
    otherData = {}, 
} 
  
function _(plr) 
    if (playerTable) then 
        local data, data2, randomNumber = "Hello World", "Hello MTA", math.random(5,25) 
        playerTable.otherData[plr] = {data, data2} 
        playerTable.number[plr] = {randomNumber} 
    end 
end 
addCommandHandler("insert", _) 
  
function o_(plr) 
    if (playerTable.otherData[plr] and playerTable.number[plr]) then     
        outputChatBox(playerTable.otherData[plr][1]..", "..playerTable.otherData[plr][2]..", "..playerTable.number[plr][1], plr) 
    end 
end 
addCommandHandler("outputdata", o_) 
  

Just randomly created this as an example, code should output "Hello World, Hello MTA, (some random number from 5-25)"

Not tested.

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