I’m not sure how to explain this so this might be difficult to understand.
local Table = {}
table.insert(Table,1)
print(Table)
--The table looks like this now:
Table = {[1] = 1}
--Is there a way to make the table look like this when I insert something into it?:
Table = {1}
When you print out a table, it will look something similar to this:
[1] = "yellow"
[2] = "blue"
[3] = "green"
Which is exactly what you are describing. This will not affect your code. This is just Roblox’s way of telling you the indexes of the elements from the table that you are printing.
Your table will still function the same as if you didn’t use tabel.insert to append new values.
You are using it the wrong way, to print the value you need to use the key.
Table[key] = value
so, doing print(Table[1]) will print 2
if your table were {[1] = 2, [2] = 2} your print would work and print out 2 since it is specified there
Because it does not exist in your table, it will print nil, thats the thing, the only KEY that exists in the table is 1 and the VALUE of 1 is 2, when accessing the table it will not go by VALUE only by KEY, if you use table.find(Table, IntValue) then it will search by VALUE and print the KEY. If you want it to never print nil even if the specified KEY does not exist, you could do print(Table[IntValue.Value] or 2), which would make it print 2 if the KEY does not exist, setting it to the default value basically.
I’m not sure if it is what you’re looking for, but try table.find()
Example code:
local TableOfValues = {3, 1, 4, 7, 2}
local index = table.find(TableOfValues, 2)
if index then --Value exists in table
print(TableOfValues[index])
--prints 2
end
When you do Table[index], you’ll get the value associated to the key. In your table, the value associated to the key 1 is 2. You do not have any value associated with the key 2, since you don’t have 2 items.
You can use table.find() to get the key of a specific value.