Remove bracket lines inside a table, and replace them with just instances

Hello there, so I’m testing with tables, and I’ve been trying to figure how to I would remove the brackets inside the table shown here:

 ["TestTable"] =  ▼  {
                       [1] = Part,
                       [2] = Value,
                       [3] = Value
                    }
                 }  

This is the code used to generate the output:

	local represent = {
		["TestTable"] = {}
	}

	local sendItems = {
		Instance.new("Part"),
		Instance.new("BoolValue"),
		Instance.new("StringValue")
	}

	for i,v in sendItems do
		table.insert(represent["TestTable"],v)
	end

	print(represent) 

Output:

 ["TestTable"] =  ▼  {
                       [1] = Part,
                       [2] = Value,
                       [3] = Value
                    }
                 }  

What I want:

 ["TestTable"] =  ▼  {
                       Part,
                       Value,
                       Value
                    }
                 }  
1 Like

If you want you can write your on print function that prints in that order. I think Luau prints like that because the values are stored in a HashMap, so even though you can define arrays, it stores like a key value pair (with the key as indices).

1 Like

Thanks so much for your reply, I think I understand what you mean.

I honestly don’t know what a HashMap is, might take a look at that.

I think the solution for this might be using a metatable.

This is just how Luau stores arrays. It is a dictionary with numbers as keys. The console displays these numbers as they are always there. What are you actually trying to achieve?

local Table = {
    [1] = "a"
    [2] = "b"
}

is the same as

local Table = {
    "a"
    "b"
}
1 Like

Uh… it’s quite complicated, for me, it is.

I’m trying to add instances onto a table, and I want to change them, like parenting them to something and whatnot.

Okay, I’m going to honest, I’m trying my best not to use GetDescendants() while trying to get every models inside something (folders)

Why are you trying to avoid it?

It lags the game, which I don’t want.

Sample code from which this all leads to:

for i,v in Equipment:GetChildren() do
		if v:IsA("BasePart") or v:IsA("Part") or v:IsA("UnionOperation") or v:IsA("MeshPart") or v:IsA("Folder") then
			weapons[v.Name] = {}
			for i2,v2 in v:GetDescendants() do
				if not v2:IsA("Attachment") and not v2:IsA("Model") and not v2:IsA("Weld") and not v2:IsA("SpecialMesh") then
					weapons[v.Name][v2.Name] = v2
				end
			end
		end
	end

What is this for? GetDescendants won’t cause lag unless you do it constantly. Another option would be to cache the results:

local Descendants = Model:GetDescendants()

-- do stuff with the Descendants variable

That’s the thing, I constantly have to use it… for certain reasons… that causes a ton of lag… fps drops…

It’s driving me crazy… is what i’m trying to express.

So, i have these weapons, and when i press Q or E, it switches

One appears, while the others don’t, that’s why I constantly have to use getdescendants to make them not appear or appear.

Okay, I need to work on optimization. O-O

Why don’t you re-parent the weapon to ReplicatedStorage when it is hidden?

local Weapons = {
    Weapon1
    Weapon2
}

local function ShowWeapon(WeaponIndex)
    for Index, Weapon in Weapons do
        if Index == WeaponIndex then
            Weapon.Parent = workspace.CurrentCamera
        else
            Weapon.Parent = game.ReplicatedStorage
        end
    end
end

ShowWeapon(1)
task.wait(2)
ShowWeapon(2)
1 Like

OH, IVE TRIED THAT!

Okay, so I have tried doing this, it just doesn’t work with the flow of my game.
Everything is welded, and when I do parent one to the character, they just drop.

Okay, maybe I should go back on trying doing that again.
Sorry, I’m just frustrated with all the lag.

You could use the Accessory class and use Humanoid:AddAccessory() to parent it to the player. This would automatically create the necessary welds and position it correctly. It requires a child part named Handle, with an attachment with a name that matches an attachment on the character rig. For tools, RightGripAttachment works best.
image

1 Like

Okay, I’ll look into this.

I appreciate your help.

I will vanquish the lag.