I’m making a script that will rapidly change the color of a box. Though the box is composed of many parts. I tried using :GetChildren() to define all the parts to one thing but it didn’t work. I may haven’t been using it correctly though. I don’t want a script, just on how I could possibly group all the parts to one word.
Hi!
First thing first, could you please show us the script you tried and how does the box look inside of the explorer?
Also, you might want to just try out for _, v in pairs() do
loop, whilst using the :GetDescendants() function.
It may look something like this:
local box = script.Parent -- Where your box is located
for _, part in pairs(box:GetDescendants()) do
if part:IsA("BasePart") then
-- Change the color
end
end
Basically what I want to do is group all the box’s to just 1 box, so I can change the color.
local Button = script.Parent
local box = workspace.Box
local box2 = workspace.Box2
local box3 = workspace.Box3
local box4 = workspace.Box4
local box5 = workspace.Box5
local box6 = workspace.Box6
local n = workspace.Press2.tick
local touchedConnection = Button.Touched:Connect(function(hit, limb)
Button.Transparency = 1
Button.CanCollide = false
Button.CanTouch = false
wait()
box.BrickColor = BrickColor.new ("Ghost grey")
n:Play()
end)
Alright, so if I get you right, the boxes are multiple BasePart’s in the workspace and you want to change color of them.
There’re actually multiple ways of achieving this.
The first way, and the one which I would probably go for, would be having the boxes stored in a table and once the button is pressed, loop through the table and change the colors.
-- Create the table of boxes
local boxes = {
workspace.Box,
workspace.Box2,
workspace.Box3,
-- and so on
}
-- To change the color
for _, box in pairs(boxes) do
box.BrickColor = BrickColor.new("Ghost grey")
end
The second way, while more straight forward, would be more chaotic; You can just go through all the boxes and change their color one by one. I do not recommend you chosing this way.
workspace.Box.BrickColor = BrickColor.new("Ghost grey")
workspace.Box1.BrickColor = BrickColor.new("Ghost grey")
workspace.Box2.BrickColor = BrickColor.new("Ghost grey")
-- ...
ok cool! I should have thought to use tables…
The color changes the box parts one at a time, not all of them at once though.
Could you show me how does the script look now?
local Button = script.Parent
local box = workspace.Box
local boxes = {
workspace.Box,
workspace.Box2,
workspace.Box3,
workspace.Box4,
workspace.Box5,
workspace.Box6
}
local n = workspace.Press2.tick
local touchedConnection = Button.Touched:Connect(function(hit, limb)
Button.Transparency = 1
Button.CanCollide = false
Button.CanTouch = false
wait()
for _, box in pairs(boxes) do
box.BrickColor = BrickColor.new("Baby blue")
n:Play()
wait(1)
box.BrickColor = BrickColor.new("Bright yellow")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Black")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Red")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Alder")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Beige")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Green")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Sea green")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Pink")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Pine Cone")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Really black")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Institutional white")
n:Play()
wait(.1)
end
end)
Ah, I see you have bunch of wait(.1)
's in your script.
The pairs loop goes through the boxes one by one.
You will have to throw this into a coroutine
.
for _, box in pairs(boxes) do
coroutine.wrap(function()
box.BrickColor = BrickColor.new("Baby blue")
n:Play()
wait(1)
box.BrickColor = BrickColor.new("Bright yellow")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Black")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Red")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Alder")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Beige")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Green")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Sea green")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Pink")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Pine Cone")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Really black")
n:Play()
wait(.1)
box.BrickColor = BrickColor.new("Institutional white")
n:Play()
wait(.1)
end)()
end
If you’d decide to go for even more effinicent way, you could store the color’s in a table and loop through them like you did with the boxes.
Thanks man!