Need help with my table.remove()

Ok, so just to be clear: I’m new to programming. Please help me if possible!  :)  

Why is my code not working? I’m trying to remove a randomly selected element from my table. However, when I run the code, I get something like: “attempt to call field ‘remove’ (a nil value)”.

I’m not sure what is wrong with the code - I think it’s something about the arguments I passed to table.remove(). 

table = {1, 2, 3, 4, 5, 6, 7, 8, 9} number1 = table[math.random(#table)] table.remove(#table, number1) print(table)

Thanks!

Line 1: You can’t name a table ‘table’. The table name is reserved for things like table.remove(). So start with renaming table to numbersTable for example.

Line 2: You are putting math.random(#numbersTable) into table with brackets. Not sure what the thinking is there. Should take it out of the brackets. To get a random number is simply math.random(theHighestValuePossible)

Line 3: table.remove is the correct function, but first argument should be the table, so the new numbersTable. No # in front, then you are passing how big the table is, not the table it self.

Line 4: “print someTable” does not give you much info except to tell you its a table. 

New code:

[lua]

numbersTable = {1, 2, 3, 4, 5, 6, 7, 8, 9} 

number1 = math.random(#numbersTable)

table.remove(numbersTable, number1)

for i = i, #numbersTable do

    print(numbersTable[i])

end

[/lua]

Ok. Thanks for your help! I will remember to do that. :slight_smile:

Line 1: You can’t name a table ‘table’. The table name is reserved for things like table.remove(). So start with renaming table to numbersTable for example.

Line 2: You are putting math.random(#numbersTable) into table with brackets. Not sure what the thinking is there. Should take it out of the brackets. To get a random number is simply math.random(theHighestValuePossible)

Line 3: table.remove is the correct function, but first argument should be the table, so the new numbersTable. No # in front, then you are passing how big the table is, not the table it self.

Line 4: “print someTable” does not give you much info except to tell you its a table. 

New code:

[lua]

numbersTable = {1, 2, 3, 4, 5, 6, 7, 8, 9} 

number1 = math.random(#numbersTable)

table.remove(numbersTable, number1)

for i = i, #numbersTable do

    print(numbersTable[i])

end

[/lua]

Ok. Thanks for your help! I will remember to do that. :slight_smile: