Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I make a variable include multiple parts?

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:

db = false

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild ("Humanoid") then
		local invisible = workspace.Part1
		if db == false then
			db = true
			script.Parent.Material = "Plastic"
			invisible.Transparency = 0
			invisible.CanCollide = true
			wait(5)
			invisible.Transparency = 1
			invisible.CanCollide = false
			script.Parent.Material = "Neon"
			db = false
		end
	end
end)

2 answers

✓ Accepted answer

Use a table. Here is how tables work:

local myTable = {game.Workspace.Part, false, 0.5} [[Use squiggle bracketsYou can put any type of data in a table.]]

[[Here is how to reference tables: ]]
myTable[1].Transparency = myTable[3]
myTable[1].Anchored  = myTable[2]

 [[The numbers were the Index values of each 
member of the table. The first member of the 
table would be index 1, and it goes on like that.
There is more to tables, but that is the basic 
stuff]]

[[You could also put parts in a group or folder, then do this:]]
local 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.

local Table = {game.Workspace.Part1, game.Workspace.Part2, game.Workspace.Part3}

for I  , v in pairs(Table) do

v.Anchored = true
v.Transparency = 0.2
v.Color = Brickcolor.new("Maroon")

print(I)

--[[ What this does is it it changes the properties of each part in the table. If you want 
to 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: ]]--

if I   == 1 or I   == 2 then
  v.Anchored = true
  v.Transparency = 0.2
  v.Color = Brickcolor.new("Maroon")
else
 v.Anchored = false
 v.Transparency = 0
 v.Color = Brickcolor.new("Electric Blue")
end

end

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

2 comments

0

Thank you so much! This is really helpful since I'm still learning.

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.

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:

local partsTable = {workspace.Part1, workspace.Part3, workspace.PartName}

if db == false then
	db = true
	for i,v in pairs(partsTable)do
		v.Transparency = 1
		v.CanCollide = true
	end
end

1 comment

0

Thank you so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.