That depends on how your model is structured, if it contains only children of parts then you can do what @stef0206 has commented.
If your model contains descendants of parts and other objects, then you might want to consider checking the ClassName
before inserting it into the list, e.g.
-- wait for model
local model = workspace:WaitForChild('SomeModel')
-- list of parts
local parts = { }
-- get all descendants
for i, obj in next, model:GetDescendants() do
-- make sure that the object is a part before inserting it into the table
if obj:IsA('BasePart') then
table.insert(parts, obj)
end
end
One thing you might also want to consider is how to structure your table too since we can use it to our advantage to make it easier to modify the blocks.
For example, if you’re making a Color Block-like game, then it would be best if you could easily access all blocks that relate to a particular color. You could do that like this:
-- wait for model
local model = workspace:WaitForChild('SomeModel')
-- list of all blocks
local allBlocks = { }
-- list of all color names
local allColors = { }
-- get all objects in a model
for i, obj in next, model:GetDescendants() do
-- check if they're a part
if obj:IsA('BasePart') then
-- get the color of the part
local color = obj.Color
-- get the hex code of that color, learn more here: https://en.wikipedia.org/wiki/Web_colors
local hexcode = color:ToHex()
-- see if we already have a table containing blocks of this color
local parts = allBlocks[hexcode]
-- if we don't create a new table
-- and set the hexcode key-value pair to the new table
if not parts then
parts = { }
allBlocks[hexcode] = parts
-- since we don't already have a list we know
-- we're looking at a new color, so let's insert that color
-- too
table.insert(allColors, hexcode)
end
-- insert it into the list fo that color
table.insert(parts, obj)
end
end
-- then, in the future when we need to set that block to invisible...
-- get length of the all colors table
local colorLength = #allColors
-- select a random hexcode color from that table
-- NOTE: you probably need to make sure you remove the colors that are already invisible
-- and/or repeat the random until you find a color that's not already transparent
local random = Random.new()
local randomColor = allColors[random:NextInteger(1, colorLength)]
local randomBlocks = allBlocks[randomColor]
for _, obj in next, randomBlocks do
obj.Transparency = 1
end
Putting it all together in a complete example:
Example
-- settings
local width = 5 -- i.e. grid width
local height = 5 -- i.e. grid height
local length = height*width -- i.e. total size of the grid
local mutationProbability = 0.4 -- i.e. how likely we are to select a new color
-- block template
local block = Instance.new('Part')
block.Name = 'BLOCK'
block.Size = Vector3.new(width, 1, height)
block.Shape = Enum.PartType.Block
block.Anchored = true
block.TopSurface = 0
block.BottomSurface = 0
block.Transparency = 0
-- prep a random number generator
local rng = Random.new()
-- get our current selected color etc
local currentColor = BrickColor.Random()
local currentHexcode = currentColor.Color:ToHex()
local currentBlockList = { }
-- our color/block tables
local allColors = { }
local allBlocks = { [currentHexcode] = currentBlockList }
-- create our model
local model = Instance.new('Model')
model.Name = 'Grid'
model.Parent = workspace
-- loop through and create our grid
for i = 1, length, 1 do
local col = math.floor((i - 1) / width) + 1
local row = (i - 1) % width + 1
-- create the part at the grid and store it in our current
-- block list defined by our current color
local part = block:Clone()
part.Position = Vector3.new(col*width, 0, row*height)
part.BrickColor = currentColor
part.Parent = model
table.insert(currentBlockList, part)
-- try to mutate the color
if rng:NextNumber() <= mutationProbability then
currentColor = BrickColor.Random()
currentHexcode = currentColor.Color:ToHex()
currentBlockList = { }
allBlocks[currentHexcode] = currentBlockList
table.insert(allColors, currentHexcode)
end
end
-- get length of the all colors table
local colorLength = #allColors
-- select a random hexcode color from that table
local random = Random.new()
local randomColor = allColors[random:NextInteger(1, colorLength)]
local randomBlocks = allBlocks[randomColor]
for _, obj in next, randomBlocks do
obj.Transparency = 1
end