Is there a way to interact with multiple part at the same time?

is there a way to select multiple part in a script with a table and doing something on them like this?

`lightTable {
a = lf.l1,
b = lf.l2,
c = lf.l3,
d = lf.l4,
}

function blackout()
lightTable(1,4).Material = metal
end`

5 Likes

note : lf is a folder, l1,2,3,4 are parts

Yes, you would just need to use a for loop. You can simply just call the children or descendants of an object.

for i, v in ipairs(Model:GetChildren()) do
    if v:IsA("BasePart") then
        v.Material = Enum.Material.Metal
    end
end
4 Likes

If I’m understanding this correctly - yes there is. You can store a reference to each Part inside a table, then iterate through that table, modifying each part as you go. For example:

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

for i, Part in pairs(Parts) do
    Part.Material = Enum.Material.Metal
end

This code is untested but in theory this will achieve what you want.

10 Likes

thanks you very much ill try this.

thanks you very much ill try both :smiley:

What @H_mzah and @DesiredFlamingFire said is the desired answer! But just wanted to add a little feature to make the parts almost LITTEARLY change at the same time, because there might actually be a small millisecond long delay between each part. To achieve that you can use spawn(), here is info.

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

for i, Part in pairs(Parts) do
     spawn(function() Part.Material = Enum.Material.Metal end))
end
2 Likes

does it work with SurfaceLights doing it like this?

lightTableSurface {
a = lf.l1.SurfaceLight,
b = lf.l2.SurfaceLight,
c = lf.l3.SurfaceLight,
d = lf.l4.SurfaceLight,
}

for i, SurfaceLight in pairs(lightTableSurface) do
SurfaceLight.Brightness. = Enum.Brightness.0
end

It should, there is really no difference. Just a different object and different property.

But there is a minor problem, the SurfaceLight.Brightness is supposed to be set to a number, describing how bright things should be, it’s not an Enum!

Enum.Brightness.0 is strange , is it Enum.Brightness = 0?

1 Like

No, you just assign SurfaceLight.Brightness to a number.

SurfaceLight.Brightness = 0

so if im right;

for i, SurfaceLight in pairs(Lights) do
SurfaceLight.Brightness = 0 ?
end

If what you’re trying to do is set multiple light’s brightnesses to something else
use a for loop like this, local script

--script.Parent.MouseButton1Click:Connect(function()
      local SurfaceLightContainer = workspace.SurfaceLightContainer

      for _,SurfaceLight in ipairs(SurfaceLightContainer:GetChildren())do
	     SurfaceLight.Brightness = 0
      end
 --end) 

you can change it to happen upon Gui button click if you want, but then it will have to be a Server Script instead.
Here surface Light Container should be a folder in workspace, with all surface lights Parented directly to it

1 Like

i see but its a table so was what i writes right? btw thanks for replying this fast :smiley:

Yes, that would be correct. There’s no such thing as Enum.Brightness, at least as far as I’m aware.

1 Like

i need it to be a table for the whole script, but thank you very much for your answer :smiley:

alright thank you very much :wink:

Do not do this. Do not wrap the property set in spawn. This may end up slightly slower as spawn has a built-in wait, so wait at its minimum is called before the property is set. It is also negligibly more expensive and grossly unnecessary.

The original code is sufficient enough and doesn’t rely on any yielding whatsoever. It sets the property and ends the current iteration immediately. A separate thread is not required to run any of these operations. Avoid spawn where you don’t need it.

3 Likes