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

How can I make a variable include multiple parts?

Asked by 4 years ago

I'm trying to make it so that when you press a button a block (Part1) becomes visible and collision is on. I've got it working for one part, but I want to be able to make a variable hold more than one part.

For example, two parts that make up a bridge. (Part 1 and Part 2). Instead of writing each individual part name, I want to be able to type a variable like "bridge" that will include all the parts.

I would union it however, in my build the Part 1 and Part 2 are different materials.

Here's all my code:

01db = false
02 
03script.Parent.Touched:Connect(function(hit)
04    if hit.Parent:FindFirstChild ("Humanoid") then
05        local invisible = workspace.Part1
06        if db == false then
07            db = true
08            script.Parent.Material = "Plastic"
09            invisible.Transparency = 0
10            invisible.CanCollide = true
11            wait(5)
12            invisible.Transparency = 1
13            invisible.CanCollide = false
14            script.Parent.Material = "Neon"
15            db = false
16        end
17    end
18end)

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Use a table. Here is how tables work:

01local myTable = {game.Workspace.Part, false, 0.5} [[Use squiggle bracketsYou can put any type of data in a table.]]
02 
03[[Here is how to reference tables: ]]
04myTable[1].Transparency = myTable[3]
05myTable[1].Anchored  = myTable[2]
06 
07 [[The numbers were the Index values of each
08member of the table. The first member of the
09table would be index 1, and it goes on like that.
10There is more to tables, but that is the basic
11stuff]]
12 
13[[You could also put parts in a group or folder, then do this:]]
14local Parts = game.Workspace.Folder:GetChildren()

Now, here is a bit of extra stuff. This is how to use the common loop you may have seen in advanced scripts, a pairs loop.

01local Table = {game.Workspace.Part1, game.Workspace.Part2, game.Workspace.Part3}
02 
03for I  , v in pairs(Table) do
04 
05v.Anchored = true
06v.Transparency = 0.2
07v.Color = Brickcolor.new("Maroon")
08 
09print(I)
10 
11--[[ What this does is it it changes the properties of each part in the table. If you want
12to make it unique for each part, I   recommend using a function with an parameter being the part, followed by parameters being the properties. But you could also do this: ]]--
13 
14if I   == 1 or I   == 2 then
15  v.Anchored = true
View all 24 lines...

First of all, how it works. In the (), you put the table you want to use for the loop. The amount of times this runs, is the amount of objects/values in the table. Second, what the I and V mean. I represents the index number, which I talked about earlier, and V a member of the table, and it works like this: If it is being looped through its FIRST time, it references the FIRST member of the table. Second time, second member. Like that. Now, instead of talking I will get to the code

0
Thank you so much! This is really helpful since I'm still learning. SobbingSabah 6 — 4y
0
I've tested this out and learned a bit from it but I'm still struggling. This goes through each part and runs the script for each part in my table one at a time. How can I use a table in a script where the parts will go in unison? For example, in my script I want the collision and transparency to go from true / 0 to false / 1 but I want all parts in my table to do this at the same time. SobbingSabah 6 — 4y
Ad
Log in to vote
0
Answered by
synkrio 281 Moderation Voter
4 years ago

You can do this by putting the parts inside an array. Then when you need to change something to the parts in the array, you can use a for loop to do that. An example would be like:

1local partsTable = {workspace.Part1, workspace.Part3, workspace.PartName}
2 
3if db == false then
4    db = true
5    for i,v in pairs(partsTable)do
6        v.Transparency = 1
7        v.CanCollide = true
8    end
9end
0
Thank you so much! SobbingSabah 6 — 4y

Answer this question