Get random part in a model

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    i want to choose in a script a random part in a model
  2. What is the issue? Include screenshots / videos if possible!
    there are models in the model

1 Like

If you have the reference to the Model then you can use QueryDescendants:

function getRandomPartInModel(model: Model): Part?
	local parts = model:QueryDescendants("BasePart")
	if #parts > 0 then
		return parts[math.random(1, #parts)]
	else
		return nil
	end
end
2 Likes

make sure to include a > in the querydescendants to only scan direct children: "> BasePart"

1 Like

Good point. Unclear whether OP is looking for a direct child or any descendant, but useful to add that information!

2 Likes