Adding objects to tables

To keep this short and simple, I am trying to place objects in arrays to get information from them.

For example,

local mod = require(game.ServerStorage.ModuleScript)
local obj1 = mod.new("Name")
local obj2 = mod.new("Name2")

local array = {
 [1] = obj1,
 [2] = obj2
}

for i = 1,#array,1 do
 print(array[i]:getName())
end

However for some reason when I use table.insert for an object, it results in it skipping the rest on the function.

local array = {}
local function addObj(obj)
 table.insert(array,obj)
 print(array[1]:getName())
end

The output in this scenario turns up blank.

Is there a way I can put objects into tables like this similar to something like Java, or is this not possible?

local array = {}

local function addObj(obj)
 table.insert(array,1,obj)
 print(array,1)
end

try this.

Was this what you were looking for?

Yes and no, it still did not work. It’s still not progressing past that point and not inserting into the table.

It might have to do with the fact that it’s an object

No, I think the for loop is just breaking it. Let me look into it.

1 Like

So I see you are defining two different arrays. What do you want me to use and not use?

The second array I used as an example to show that it wouldn’t add into the array. All I’m really needing it to do is to add the object into the table using table.insert()

Okay, so where do you want to put this object? (What place (1st, 2nd, 3rd, etc.) in the array)

first is the preferable spot, also im making this long for the character limit

local mod = require(game.ServerStorage.ModuleScript)
local obj1 = mod.new("Name")
local obj2 = mod.new("Name2")
local array = {obj1,obj2}

local function addObj(obj)
    local obj = mod.new("Name3")
    table.insert(array,1,obj)
    print(array[1]:getName())
end

for i = 1,#array,1 do
print(array[i]:getName())
end

Try this.

1 Like

Did it work? Adding so I can pass character limit

Yes. Thanks man I really appreciate it!

Np! Glad to help anytime! :smiley:

Make sure you mark the script i put as the solution so people know that this topic has been closed!

Well now there is another issue, it’s overwriting all the previous objects to be Name3.

table.insert() takes in Table, Index, and Value

So in your code

you are adding the object to the Same Index every time.
So, it’s overwriting that index.

So you can switch table.insert(array,1,obj) with table.insert(array,#array+1,obj) where the # operator will return the number of elements in an array. So you are basically giving a new index position each time by adding 1.

It’s also overwriting the others too though. I tried printing the first object’s name before the addobject method and it said it was the second one despite what is shown in the array. They are overwriting each other.

What is your Source Code for adding objects though? You have given us the addobj function. But how are you adding objects to it? Can I see the source code for that?

Main script:

local mod = require(game.ServerScriptService.Slot)
local obj1 = mod.new("Name")
local obj2 = mod.new("Name2")
local array = {obj1,obj2}
print(array[1]:getName())

Module

local slot = {}

slot.__index = slot

function slot.new(index)
	local newSlot = {}
	setmetatable(newSlot,slot)
	slot.Index = index
	
	return newSlot
end

function slot:getName()
	return self.Index
end

return slot

Output says Name2

local mod = require(game.ServerStorage.ModuleScript)
local obj1 = mod.new("Name")
local obj2 = mod.new("Name2")
local array = {obj1,obj2}

local function addObj(obj)
    local obj = mod.new("Name3")
    table.insert(array,1,obj)
    print(array[1]:getName())
end

addObj()

for i = 1,#array,1 do
print(array[i]:getName())
end

try that

1 Like