Table: What does this mean?

Dont flag this because there is not much to say about:

I cant follow this:

if [ArmorType][HitPart.Name] ~= nil then
print(“hey”)
end

what is [][] doing? or [][][]

1 Like

That’s just how you index a table

Table.test is the same as Table['test']

Ex:

local Table = {
    test = "hi"
}

print(Table.test) -- > "hi"
print(Table['test']) -- > "hi"

You didnt answer me about [][][][]

Basically ArmorType is a table. Then we are checking if HitPart.Name is in the table. Say we had this table that tells us what type of armor does what amount of protection.


local ArmorType = {

["Wooden Armor"] = 100,
["Stone Armor"] = 200,
["Diamond Armor"] = 500

}

local sword = --

sword.Touched:Connect(function(HitPart)
     print [ArmorType][HitPart.Name]
      -- if it touched "Wooden Armor", then it would print out 100
end)

Hello! I think you are talking about something within a post I Made Earlier!

If so, you could have PM’ed me, and I would have gladly explained! First, here is some code, for context:

settings.Damage = {

	["Default"] = {
		--Head (R15+R6)
		["Head"] = 16,
		--Handle (Hat)
		["Handle"] = 3,
		--RootPart (Torso Extension, It's where the torso is, and should have the same damage as the Torso to prevent bugs.)
		["HumanoidRootPart"] = 16,
		--R15
		["UpperTorso"] = 16,
		["LowerTorso"] = 16,
		["LeftUpperArm"] = 8,
		["LeftLowerArm"] = 6,
		["LeftHand"] = 3,
		["RightUpperArm"] = 8,
		["RightLowerArm"] = 6,
		["RightHand"] = 3,
		["LeftUpperLeg"] = 12,
		["LeftLowerLeg"] = 8,
		["LeftFoot"] = 4,
		["RightUpperLeg"] = 12,
		["RightLowerLeg"] = 8,
		["RightFoot"] = 4,
		--R6

		["Torso"] = 16,
		["Right Arm"] = 8,
		["Left Arm"] = 8,
		["Right Leg"] = 8,
		["Left Leg"] = 8

	};
};

This stuff is within something called a Table. Tables contain information, as shown in the above. That “[]” is how you Index that table’s data.

Here is a demonstration:

Table = {

["InternalTable1"] = {
     ["Value"] = 10,
     ["Value2"] = 20,

}
}

NewExample = Table["InternalTable1"]
--[[
--Returns:
     ["Value"] = 10,
     ["Value2"] = 20,
]]--

NewExample = Table["InternalTable1"]["Value"]
--Returns:
--10

NewExample = Table["InternalTable1"]["Value2"]
--Returns:
--20

NewExample = Table["InternalTable1"]["Value3"] --Dosen't Exist
--Returns:
--nil

Hope this explains it, if not read the documentation I linked above! :wink:

2 Likes

TableA["Example"] actually Does exist.

2 Likes

Wow, I honestly never knew that! I’ll edit my post.

I have never seen that way of using square brackets and when I put it in a script it tells me that it is a syntactic error. So I guess there is no such form of notation. Most probably it would have been if ArmorType[HitPart.Name] ~= nil then, without the brackets.

It was referencing a post @GreenBoy657_Gaming made earlier, the conditional actually looks like this:

if SettingsService.Settings.Damage[ArmorType][HitPart.Name] ~= nil and ...

Although you are correct in that indexing a table by wrapping the table itself in square brackets is not valid syntax.

1 Like