Damagex443 recently helped with a script of mine and he did something I never saw with tables. He indexed "i" with the variable "Clone" and then put it in the table "ClonedArms"(line 22),is this another way to insert values into a table or what? I'm really confused.
game.Players.PlayerAdded:connect(function(Player) Player.CharacterAdded:connect(function(Character) --------------------------------------------------------- function MakeDisplayArms() wait() --------------------------------------------------------- local CurrentCamera = game.Workspace.CurrentCamera local PlayerArms = { Character["Right Arm"], Character["Left Arm"]} local ClonedArms = {} --------------------------------------------------------- for i,v in pairs (PlayerArms) do Character:WaitForChild(v.Name) local Clone = v:Clone() Clone.Parent = CurrentCamera--Hides it from everyone else Clone.Name = "Cloned_"..v.Name --Renames it Clone.CanCollide = true local Weld = Instance.new("Weld",Clone) Weld.Part0 = Clone Weld.Part1 = v ClonedArms[i] = Clone end return ClonedArms end --------------------------------------------------------- Arms = MakeDisplayArms() for i,v in pairs (Arms) do print(v) end end) end)
My questions/thoughts:
. How could he replace a index with a part's name?
. Can't a index only be numbers?
. How did he insert the variable "Clone" in the table without using table.insert?
Alright, first of all, sorry that I didn't elaborate enough. Let's walk through this together. First of all, we declare a table.
local MyTable = {} -- This declares a new, empty table
Now lets create a Part, and put it in a table
local Part = Instance.new("Part", Workspace) -- Instance a part and set it's parent to workspace table.insert(MyTable, Part) -- This will insert the part at the end of the table
This is the easiest way to add single Instances(Game Objects), Strings, Integers, booleans etc. to a Table. In your case, you have more then one Part you want to add to the Table, so let's recreate this. For example we want to add four new Parts to a table. We first create the four Parts, in your case this is two representing the cloned arms.
local PartsToAdd = 4 -- Ammount of Parts to add for i = 1, PartsToAdd do -- The 'i = 1' stands for either index or iteration. local Part = Instance.new("Part", Workspace) -- Create a new Part Part.Name = "Part "..tostring(i) -- This will give the parts a name. 'Part_1', 'Part_2', 'Part_3' etc. MyTable[i] = Part -- And we add the part to the table at location i, which is the number of the current iteration/index of the for loop. So 1 to 4. This can also be done with table.insert(Part, Workspace), because in this case it will just work the same. end
Now we have the following table
MyTable Index [1] [2] [3] [4] Value Part_1 Part_2 Part_3 Part_4
and you can call or change a specific Value, from a specific index.
print(MyTable[3].Name) -- This will print 'Part_3'. Notice how I added the property 'Name' because parts are instances and they can't be used as strings in most cases. MyTable[3] = "This is a string" print(MyTable[3]) -- This will print 'This is a string'
I hope this is clear for you, otherwise don't hesitate to ask more. There are loads of more possibilities for you to discover using Tables.