Help With Fitting a Part in Another

I’m looking for a formula to fit a part that can be any size into another part,
Basically scale a part until it fits into the other,
I need this for a Vr Backpack system any help would be appreciated

Can you describe in a bit more detail what you mean by that?

What are you making and how would this fit into that? Can you give some examples of setups and the intended result?

Example: I’m trying to fit a part thats Size: 1, 4, 3 in to a part thats Size: 2, 2, 2,
to do that i need to make the Part I’m fitting in Size: .5, 2, 1.5 Original: 1, 4, 3,
I’m trying to find something to do that automatically so i don’t need to do it manually

If you want to fit it within a max number, you could use math.clamp

So how’d you apply this to size:

local BagSize = Vector3.new(3,3,3) -- the size of the bag
local ItemSize = Vector3.new(5, 8, 2) -- the item that will be fit to the bag (in this case it's much bigger than the bag)

local ClampedSize = Vector3.new(
	math.clamp(ItemSize.X, 0, ItemSize.X), -- clamp the item's X within the bag's X
	math.clamp(ItemSize.Y, 0, ItemSize.Y), -- clamp the item's Y within the bag's Y
	math.clamp(ItemSize.Z, 0, ItemSize.Z) -- clamp the item's Z within the bag's Z
)

You could find the largest difference in size (as a ratio, per axis) and then scale based on that:

local a = part.Size
local b = box.Size
local largestRatio = math.max(a.X/b.X, a.Y/b.Y, a.Z/b.Z)

local newSizeOfPart = a / largestRatio

You can check that largestRatio > 1 if you only want to shrink the part and not also grow it.

1 Like

I would also like to add that you should maybe have a variable for an offset, so that it shrinks it to not fit EXACTLY but to have some padding on the sides.

So if you want 10% padding you could do


local paddingPercentage = 10
local a = part.Size
local b = box.Size
local largestRatio = math.max(a.X/b.X, a.Y/b.Y, a.Z/b.Z)

if Consent then
  local newSizeOfPart = a / largestRatio / (paddingPercentage/100)
end

Exactly what i was looking for thank you