Can't add to a table

I am trying to add to a table every time a value is added to a folder, here is the code:

local store=game.ServerStorage.store
local table={}
store.ChildAdded:Connect(function(child)
	local value = child.Value
	table.insert(value)
end)

However, this gives me the error “attempt to call a nil value” on this line:

table.insert(value)

I can’t figure out why this is happening. TIA for help.

1 Like

You need to give it the table to add to in the first argument. Also your naming of the variable is overwriting the table class. Do this

local ServerStorage = game:GetService("ServerStorage")

local tbl = {}

ServerStorage.ChildAdded:Connect(function(child)
	table.insert(tbl, child.Value)
end)

Prevents overwriting as tbl is not a class, unlike table, and properly works as how table.insert works, especting the table to add to in the first argument, and the thing to add in the second

1 Like

The table library is the thing that contains functions that operate on tables. When you have a table, you need to provide it to the functions in the table library. They are not automatically included in every table.

local groceries = {}
table.insert(groceries, "Milk")
table.insert(groceries, "Eggs")
table.insert(groceries, "Bread")
table.insert(groceries, "Probably a vegetable of some kind")

Edit: and as @EmbatTheHybrid described, your naming the table table is preventing access to the table library itself - this is called variable shadowing.

2 Likes

You’ll need to put the table you want to insert it into. Also you’ll need to change your table variable name.

local store=game.ServerStorage.store
local t={}

store.ChildAdded:Connect(function(child)
	local value = child.Value

	if value then -- just incase
       table.insert(t, value)
    end
end)

While your help is appreciated, you really ought to try running code before suggesting it. There’s a number of issues with what you suggested and we probably don’t want to mislead anyone who might be confused already.

1 Like

I thought it could work, but it seems like it wont work, so I just deleted the post.