Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to scale models properly through script using Attributes?

So anyways

I need to scale models using attributes

which will be a vector3 value

these are the same 2 models with different vector3 values in size for the attribute

how would i properly scale them without using anything else like an iv loop to make them the same size of each part

For example if a model is 10 ,10,10

and another model i want to scale to is 50,50,50

I want the model to become from 10,10,10 to 50,50,50

while still maintaining the scale of all the parts in the model

I tried dividing 50/5 and using the value of 5 to multiply the size of 50,50,50 to every single part

now the problem is that since i do this to every part it ends up becoming bigger

does anyone know how I can get this to work?

1 answer

✓ Accepted answer

Good news for you, Roblox just announced that they will add a new Model:ScaleTo() function 8 days ago from this post.

local size = 10
Model:ScaleTo(size)

However, Roblox hasn't officially released it yet as it is still pending (from this page). Be patient until it goes live.

In the meantime, try this code that I got from this guy. (credits btw)

function ScaleModel(model, xScale, yScale, zScale)
	local cf, _ = model:GetBoundingBox()
	local origin = cf.Position

	local scale = Vector3.new(xScale, yScale, zScale)

	for _, obj in pairs(model:GetDescendants()) do
		local OriginalSize = obj:GetAttribute("OriginalSize")
		if OriginalSize == nil then
			if obj:IsA("BasePart") then
				obj:GetAttribute("OriginalSize", obj.Size)
			elseif obj:IsA("JointInstance") then
				obj:GetAttribute("OriginalSize", Vector.zero)
			elseif obj:IsA("SpecialMesh") then
				obj:GetAttribute("OriginalSize", obj.Scale)
			end
			OriginalSize = obj:GetAttribute("OriginalSize")
		end

		local DontScale = obj:GetAttribute("DontScale")
		if DontScale == nil then
			if obj:IsA("BasePart") or obj:IsA("SpecialMesh") then
				obj:SetAttribute("DontScale", false)
				DontScale = obj:GetAttribute("DontScale")
			elseif obj:IsA("JointInstance") then
				DontScale = false
			else
				DontScale = true
			end
		end

		if DontScale == true then return end
		
		if obj:IsA("BasePart") then
			local newSize = OriginalSize * scale
			obj.Size = newSize
			
			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("SpecialMesh") then
			local newScale = OriginalSize * scale
			obj.Scale = newScale
		end
	end
	return model
end

This discussion is preserved as it was. New answers, comments, and votes are closed.