ManeXi Posted August 25, 2016 Share Posted August 25, 2016 (edited) 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 August 25, 2016 by Guest Link to comment
Bonsai Posted August 25, 2016 Share Posted August 25, 2016 What is number? If you use it like this, its a variable, so you cannot do something like playerTable.number. You can only do that if you use it like this: playerTable["number"] = {}. Link to comment
ManeXi Posted August 25, 2016 Author Share Posted August 25, 2016 Number is the variable used to call a number, the index of the table that I created, and no "number" can't be a string cause various sub-tables will be added to "playerTable" Link to comment
StefanAlmighty Posted August 25, 2016 Share Posted August 25, 2016 playerTable = {} playerTable[1] = {} Above is the same as: playerTable = { [1] = { } } To add to it: table.insert(playerTable[1], content) Link to comment
aka Blue Posted August 25, 2016 Share Posted August 25, 2016 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
zneext Posted August 25, 2016 Share Posted August 25, 2016 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. Link to comment
novo Posted August 25, 2016 Share Posted August 25, 2016 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
ManeXi Posted August 25, 2016 Author Share Posted August 25, 2016 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
Ab-47 Posted August 26, 2016 Share Posted August 26, 2016 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now