Inserting into tables

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}
1 Like

U mean like this?

local Table = {}
table.insert(Table, 1)
setmetatable(Table, { __tostring = function(t) return "{" .. table.concat(t, ", ") .. "}" end })
print(Table)  -- Output: {1}
1 Like

Table = {[1] = 1}
and
Table = {1}

are the same thing

1 Like

Yes, but there’s another problem.

local Table = {}
table.insert(Table,2)
setmetatable(Table, { __tostring = function(t) return "{" .. table.concat(t, ", ") .. "}" end })
print(table.find(Table,2))--prints out 1

Why does it print out 1?

Yes, I’m well aware. charssssss

Try this

local Table = setmetatable({}, { __tostring = function(t) return "{" .. table.concat(t, ", ") .. "}" end })
table.insert(Table, 20)
print(Table)

I think you missed the table.find part.

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.

1 Like

So like, what are you trying to do if both tables represent the same thing?

Unfortunately it does affect my code.

local Table = {[1] = 2}
print(Table[2]) --nil

If you know of any other way to not print out nil then I would really appreciate that.

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

1 Like

I fully understand what you mean. I just simplified my version.

local Table = {[1] = 2}
local IntValue = script.IntValue -- This is 2
print(Table[IntValue.Value]) --nil

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.

1 Like

It is nil because there is no index with the Value ‘2’

1 Like

I know what the problem is. I just don’t know how to fix it. If you know of a way to find the 2 inside the table then feel free to tell me.

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

Sorry for lack of indentation, I’m not on PC.

1 Like

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.

OMG thank you SO much! I’ve been trying to fix this for 4 hours. You’re a lifesaver.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.