Table.insert() does not work as expected

  1. What do you want to achieve? I want to create a table of all models within a folder.
  2. What is the issue? When creating the final table, the table consists of the number of elements expected instead of consisting of a list of models.
    Here is my code, simplified;
local Structs = script.structs -- Structs is a Folder of Models.
local function GetModel(inp)
    return inp.Name
end
local models = {}
for _,thing in pairs(Structs:GetChildren()) do
    table.insert(models, #models+1, GetModel(thing))
end
print(models)

READ: TO SEE THE TABLE WHEN IT IS PRINTED, YOU MUST HAVE EXPRESSIVE OUTPUT ENABLED.

Expected:

{ "Model1", "Model2", "Model3", "Model4" }

Actual output:

{4}

Why would the final output be the number of the expected elements? Am I using table.insert() wrong? Please help, as this confuses me big time.

Table insert seems to be working, I’m not sure as to why you used print(TableName) you could use a for loop.

Expressive Output allows for a better way to see tables; no for loop needed.

Lua uses natural number sequencing, not whole number sequencing. Fixed code:

local Structs = script.structs -- Structs is a Folder of Models.
local function GetModel(inp)
    return inp.Name
end
local models = {}
for _,thing in pairs(Structs:GetChildren()) do
    table.insert(models, #models, GetModel(thing))
end
print(models)

This is completely wrong, as that will overwrite the last element of the table every time. The point of the +1 is to add an element after the last element, not into the last element.

He is right, the +1 adds to the end of it, this may need further explanation by someone of a Top Contributor or Roblox Staff role.

1 Like

The table index is an optional argument. If you just want to insert at the first free numerical index, do

table.insert(models, GetModel(thing))

If you want to append and ignore nil values, do

models[#models+1] = GetModel(thing)

Your line is kind of like the worst of both worlds.

Edit: Why do any of this in the first place? You can just do

local models = Structs:GetChildren()
2 Likes

He/She is not looking for alternatives, He/She is looking to learn. This could be useful in certain areas later.

EDIT: or that’s atleast how it seems. The question was quite specific.

As I said, the code was VERY simplified, down to the main problem. I am making a table of custom objects, those custom objects being tables, which contain custom objects that represent a part within the model. Summary: A heavily customized system for a system I am creating myself.

This is not true, I am looking for an alternative OR solution, one that will work.
Here is my full code to satisfy why I need this method:

-- Helperbot3 <3 --
repeat wait() until script:FindFirstChild("structs")
local Structs = script:FindFirstChild("structs")
local CToolsReplicatedStorage = Instance.new("Folder", game.ReplicatedStorage)
CToolsReplicatedStorage.Name = "CToolsReplicatedStorage"
local ServerReady = Instance.new("BoolValue", CToolsReplicatedStorage)
ServerReady.Name = "ServerReady"
local GetModels = Instance.new("RemoteFunction")
GetModels.Name = "GetModels"

local function NewModel(model)
	local function NewPartObject(part)
		local objb = {
			{part.Name},
			{part.Anchored},
			{Vector3.new(0, 0, 0)},
			{part.Rotation},
			{part.CanCollide},
			{part.Transparency},
			{part.Reflectance},
			{part.Size}
		}
		return objb
	end
	local function NewPart(primary, part)
		local obja = {
			{part.Name},
			{part.Anchored},
			{part.Position - primary.Position},
			{part.Rotation},
			{part.CanCollide},
			{part.Transparency},
			{part.Reflectance},
			{part.Size}
		}
		return obja
	end
	local primary = model.PrimaryPart
	if(primary ~= nil)then
		local primaryPart = NewPartObject(primary)
		local modelx = {primaryPart}
		for _,i in pairs(model:GetChildren())do
			if(i:IsA("BasePart"))then
				table.insert(modelx, #modelx+1, NewPart(primary, i))
			end
		end
	else
		error("Any additonal models require a primary part to be set. The primary part can be any part within the model.")
	end
end
local function getModels()
	local models = {}
	for _,thing in pairs(Structs:GetChildren()) do
		models[#models+1] = NewModel(thing)
	end
	return models
end

GetModels.OnServerInvoke = nil
local function createModelFromModelObject(obj, startingVector3)
	local function createPartFromPartObj(partObj)
		local part = Instance.new("Part", workspace)
		local pos = startingVector3 + partObj.RelativePosition
		local rx, ry, rz = partObj.Rotation.X, partObj.Rotation.Y, partObj.Rotation.Z
		part.Name = partObj.Name
		part.Anchored = partObj.Anchored
		part.CFrame = CFrame.new(pos) * CFrame.Angles(math.rad(rx), math.rad(ry), math.rad(rz))
		part.CanCollide = partObj.CanCollide
		part.Transparency = partObj.Transparency
		part.Reflectance = partObj.Reflectance
		part.Size = partObj.Size
		return part
	end
	local model = Instance.new("Model", workspace)
	for _,object in pairs(obj) do
		local objx = createPartFromPartObj(object)
		objx.Parent = model
	end
	return model
end
local function createViewFromModelObject(obj, startingVector3, transparency)
	local objx = obj
	for _,part in pairs(objx) do
		part.Transparency = transparency
	end
	return createModelFromModelObject(objx, startingVector3)
end
ServerReady.Value = true
local models = getModels()
print(models)

Then you could simply :GetChildren as @emojipasta suggested.

You completely misunderstood my point. GetChildren() does not satistfy my custom system’s requirements.

You aren’t supposed to print a table, you’re supposed to do this if you want to print all of the children:

for i, v in pairs(models) do print(v)

Also you don’t need a local function to get an instances name haha.

identical to

local function getModels()
	local models = Structs:GetChildren()
	for i,thing in pairs(models) do
		models[i] = NewModel(thing)
	end
	return models
end

?

If you read my first message in the post, He/She is 100% aware that they could use a for loop.

1 Like

Yes, that is correct. I replaced models[i] = NewModel(thing) with models[#models+1] = NewModel(thing) when testing your first message.

Sorry, didn’t catch that. (30 char)

I don’t think you guys understand: You can print a table with Expressive Output. I do it all the time, and so do others. Please do not fixate on things that are already identified as not a part of the problem.

That doesn’t matter. I don’t need it, but I don’t not need it either. Making fun of my coding style does not help this subject, even a tiny bit.

Why are you printing the saved instances and not the instance names? This could be the reason why.

Your NewModel function isn’t returning anything.

1 Like

Wow, that makes me want to slam my head. I can’t believe I didn’t see that. I tested and now it works fine. Unbelievable, literally a beginner’s mistake.

Thank you so much! :slight_smile:

1 Like