# Randomizing the size of every part in a model all together

I’m trying to make little worms always have a different size when spawned but when I use this script, each part of the worm is different size since it consists of 12 cubes. Can anyone tell me how I can make it so all of the parts are the same size.

I’ve tried multiple different ways revolving around math.random but I can’t get the right one.

``````	for i, wormS in pairs(worm:GetChildren()) do
if wormS:IsA("Part") then
local mR = math.random(4,7)/10
if mR == 0.4 then
print("4")
wormS.Size = Vector3.new(0.4,0.4,0.4)
end
if mR == 0.5 then
print("5")
wormS.Size = Vector3.new(0.5,0.5,0.5)
end
if mR == 0.6 then
print("6")
wormS.Size = Vector3.new(0.6,0.6,0.6)
end
if mR == 0.7 then
print("7")
wormS.Size = Vector3.new(0.7,0.7,0.7)
end
end
end
``````

Is `worm:GetChildren()` a single worm? If it is, your for loop is going through each part in the worm and setting the size of the individual parts with a new random number.

1 Like

try to use `for loop` and `:GetDescendants`
and use `BasePart` instead of `Part`

``````--loop to get descendants
for i,v in pairs(worm:GetDescendants()) -- get everything that is Descendant of worm
if  v:IsA("BasePart") then -- Check if it is BasePart#include(MeshPart,Part,Sphere,Cylinder)
-- Do something
local mR = math.random(4,7)/10
v.Size = Vector3.new(mR,mR,mR) -- Shorten the code
end
end
``````

I hope this help you, let me know if it doesn’t work

It still randomises each part of the worm individually rather than making all the parts the same size. I’m debating on just scrapping the script and just make it take a random worm with the preset sizes from a folder, rather than taking a single worm model and changing the size through a script. Still I’d like to figure out an answer to the problem for future reference and learning.

Yeah it’s a single worm. Since the worm is a model and has a humanoid inside, I have to filter it with a :GetChildren() or else it just refuses to work so I don’t know what to do regarding that. Only way I could think of going around that is the way I used which didn’t work.

Oh Yes, I forgot to move `local mR` outside the loop.
try this

``````    --loop to get descendants
local mR = math.random(4,7)/10
for i,v in pairs(worm:GetDescendants()) -- get everything that is Descendant of worm
if  v:IsA("BasePart") then -- Check if it is BasePart#include(MeshPart,Part,Sphere,Cylinder)
-- Do something

v.Size = Vector3.new(mR,mR,mR) -- Shorten the code
end
end
``````
2 Likes

All you need to do is set the random number outside the loop. Your code was setting a new random number each iteration, so a new size was determined for each part in the worm.

``````local mR = math.random(4,7)/10
for i, wormS in pairs(worm:GetChildren()) do
if wormS:IsA("Part") then

if mR == 0.4 then
print("4")
wormS.Size = Vector3.new(0.4,0.4,0.4)
end
if mR == 0.5 then
print("5")
wormS.Size = Vector3.new(0.5,0.5,0.5)
end
if mR == 0.6 then
print("6")
wormS.Size = Vector3.new(0.6,0.6,0.6)
end
if mR == 0.7 then
print("7")
wormS.Size = Vector3.new(0.7,0.7,0.7)
end
end
end
``````
1 Like