Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with understanding a loops index?

Asked by 9 years ago

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?

1 answer

Log in to vote
2
Answered by 9 years ago

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.

0
If I wanted to remove the arms from "ClonedArms" everytime the player dies,would I just remove it with ":destroy()" or will I have to remove it off the table? kevinnight45 550 — 9y
1
Well, if they should be back on respawn, then you'll just have to destroy them. The script you have above will then overwrite the old table with the new values, but if you really want to remove everything then you should empty the table too. You can do this by building another for loop that iterates over each child. Check if the name and childname match and then use table.remove(ClonedArms, i). damagex443 325 — 9y
Ad

Answer this question