I'm having an issue getting a colour from a random object

Hello, I’m having issues printing a random object’s colour.

  1. What do you want to achieve? A script that prints the model’s brickcolour chosen from a math.random()

  2. What is the issue? “choseone” has the same name as “model” should be and prints the correct name, but “chose” prints nil even though the model exists

  3. What solutions have you tried so far? I have tried to use print(model[num]) but it did not work.

local num = math.random(1,4)

while true do
	for i,model in pairs(script.Parent:GetChildren()) do
		if model:IsA("Model") then
			local chosenone = "Shape" .. num
			local chose = model:FindFirstChild(chosenone)
			print(chose.Name)
		end
	end
	wait()
end```

Thank you for reading!
1 Like

Can you send a screenshot of your explorer window with the models? FindFirstChild will only return nil if the part doesn’t exist, so I am guessing you got the name of the part wrong.

2 Likes

The only way this can work is if all the Models have all 4 Shapes?
Ie Shape1-4. If any model doesn’t have all shapes, it will likely return Nil.

This screenshot shows it working only because all the random ‘Shapes’ (1-4) exist in the two models. (all my shapes are stone grey)

I say Likely because if (for instance) it contains only Shape1, theres a 1 in 4 chance it will work

2 Likes

Hello, sorry for the late reply, this is my model structure:
image

the math.random() chooses a random number and sets it as the “choseone” variable with the Shape name and finds it and prints its colour

Looking again, here’s another example of code that may help you. It may not be precisely the answer but it might help you work out another way of tackling your solution.

It uses the random number to get the ‘Shape’
It gets the Model of the ‘found shape’
It prints the Brickcolor of that ‘found shape’

I hope this helps, if it does I can cut paste the code