How to call all blocks inside a model

Hello there!. I was wondering how can I call blocks inside a model.

  • I would like to make a “math.random()” function where it chooses any model. The model selected gets all of its children (blocks) and make them transparent.

I used GetChildren() and GetDescendants().

Here is my script.

 local blue = script.Parent.Blue
local red = script.Parent.Red
local Yellow = script.Parent.Yellow

local ColorDisappearing = math.random(1, 3)

if ColorDisappearing == 1 then
	wait(5)
	blue:GetDescendants().Transparency = 1
	blue:GetDescendants().CanCollide = false
end

if ColorDisappearing == 2 then
	wait(5)
	red:GetDescendants().Transparency = 1
	red:GetDescendants().CanCollide = false
end

if ColorDisappearing == 3 then
	Yellow:GetDescendants().Transparency = 0
	Yellow:GetDescendants().CanCollide = false
end

image

The output doesn’t write any mistake or warning with my script, it only prints plugin errors.

Any help is appreciated :slight_smile:

Put all of the models in a common location (parented to the same instance), e.g. place them all under one folder. From there you can easily access a random model using array indexing and :GetChildren(), as so:

local models = modelFolder:GetChildren()
local randModel = models[math.random(#models)]

You can make all the parts transparent by simply looping through the model’s descendants and setting the transparency of each BasePart instance.

1 Like
local Parts = workspace.FolderName:GetDescendants();

for i = 1,1 do
    for i,v in pairs(Parts) do
        if v:IsA("Part") then
           v.Transparency = math.random(1,3);
          end;
     end;
end;
1 Like

Use @rapmilo’s suggestion to easily select a random model, then combine it with @Interactivated’s method to change each part in the model.

1 Like

Thank you all! I’m gonna use both methods :smiley: