Revamped model resize function

So since the popular model resize plug in hasn’t been updated in years I decided to code it by scratch to cover instances that don’t get resized automatically by the plug in, such as attachments, or particle effects.

function scaleModel(model, scale)
	local origin 	
	if model.PrimaryPart ~= nil then
		origin = model.PrimaryPart.Position
	else
		local cfr,size = model:GetBoundingBox()
		origin = cfr.Position
	end
	for _, obj in ipairs(model:GetDescendants()) do
		if obj:IsA("BasePart") then
			obj.Size = obj.Size*scale
 
			local distance = (obj.Position - origin)
			local rotation = (obj.CFrame - obj.Position)
			obj.CFrame = (CFrame.new(origin + distance*scale) * rotation)
		elseif obj:IsA("JointInstance") then
			local c0NewPos = obj.C0.p*scale
			local c0RotX, c0RotY, c0RotZ = obj.C0:ToEulerAnglesXYZ()
 
			local c1NewPos = obj.C1.p*scale
			local c1RotX, c1RotY, c1RotZ = obj.C1:ToEulerAnglesXYZ()
 
			obj.C0 = CFrame.new(c0NewPos)*CFrame.Angles(c0RotX, c0RotY, c0RotZ)
			obj.C1 = CFrame.new(c1NewPos)*CFrame.Angles(c1RotX, c1RotY, c1RotZ)
		elseif obj:IsA("Beam") then
				obj.Width0 = obj.Width0*scale
				obj.Width1 = obj.Width1*scale
				obj.CurveSize0 = obj.CurveSize0*scale
				obj.CurveSize1 = obj.CurveSize1*scale
				obj.TextureLength = obj.TextureLength*scale
		elseif obj:IsA("ParticleEmitter") then
			local sizeNS = obj.Size.Keypoints
			print(sizeNS)
			local newNS = {}
			for i,v in pairs(sizeNS) do
				table.insert(newNS,NumberSequenceKeypoint.new(v.Time,v.Value*scale))
			end
			obj.Size = NumberSequence.new(newNS)
			obj.Speed = NumberRange.new(obj.Speed.Min*scale,obj.Speed.Max*scale)
			obj.Acceleration = obj.Acceleration*scale
		elseif obj:IsA("RopeConstraint") then
			obj.Length = obj.Length*scale
			obj.Thickness = obj.Thickness*scale
		elseif obj:IsA("Attachment") then
			obj.Position = obj.Position*scale
		end		
	end
end

I used the code from Is this the best way to scale a model? - #26 by AKASGamingYT as a starting point. Problem is that when I apply this to model with welds inside, some parts, I suspect only unions, are rotated 90 degrees in the Y axis when I call this script. Anyone know what might be the problem with this code for that to happen?

1 Like