How to scale a detailed model during runtime?

hi, ive been suffering greatly for 5 hours.

I am trying to create a function that can be used to scale character models using a given scale factor. (For example, 0.5 to half its size or 2 to double its size). The models are entirely custom creatures containing humanoids–not roblox R6/R15 models–which contain Motor6Ds and WeldConstraints. I’ve been trying for hours to figure this out with various functions but keep encountering different issues.

Presently I’ve arrived at this code, which works for all models when put into the console when not playing the game, but when used on the player’s character in-game it works only for some models and not others and I have no idea why. For the models for which it doesn’t work, parts which have their position directly set (rather than being altered as the C0/C1 of a joint) are all displaced vertically by the same unknown distance.

local function scaleModel(model, scaleFactor)
	local dilationCentre = model.PrimaryPart.Position

	local WeldConstraints = {}

	for _, d in pairs(model:GetDescendants())  do
		if d:IsA("JointInstance") then
			d.Part0:SetAttribute("Jointed", true)
			d.Part1:SetAttribute("Jointed", true)
		elseif d:IsA("WeldConstraint") then
			WeldConstraints[d] = {d.Part0, d.Part1}
			d.Part0 = nil d.Part1 = nil
		end
	end

	for _, d in pairs(model:GetDescendants()) do
		if d:IsA("BasePart") then
			d.Size = d.Size * scaleFactor
			if not d:GetAttribute("Jointed") then
				d.Position = ((d.Position - dilationCentre) * scaleFactor) + dilationCentre
			end
		elseif d:IsA("JointInstance") then
			d.C0 = d.C0 + d.C0.Position*(scaleFactor - 1) 
			d.C1 = d.C1 + d.C1.Position*(scaleFactor - 1)
		end
	end

        -- Restore WeldConstraints
	for WeldConstraint, Attachments in pairs(WeldConstraints) do
		WeldConstraint.Part0 = Attachments[1]
		WeldConstraint.Part1 = Attachments[2]
	end
end

I’ve been scouring the forums for hours to no avail. Can anyone help me fix this function, or recommend a public module or plugin that would allow me to resize complex character models during runtime?

Pictures/videos of what is happening with the posted code would be nice.

1 Like

Oh well, doesn’t matter. Point and case, I believe you are simply overcomplicating things.
I ran the code below with a server script inside of a character on a custom rig and it worked with no problems.

local scale = 6
local char = script.Parent
local hum = char:FindFirstChildWhichIsA("Humanoid")
hum.HipHeight = hum.HipHeight * scale
for i,v:Instance in pairs(char:GetDescendants()) do 
	if v:IsA("BasePart") then
		local v : BasePart = v
		v.Size = v.Size * scale
	end
	if v:IsA("Motor6D") or v:IsA("Weld") or v:IsA("JointInstance") then 
		local v : Motor6D = v
		local c = v.C0
		v.C0 = CFrame.new(c.X*scale,c.Y*scale,c.Z*scale) * v.C0.Rotation
		c = v.C1
		v.C1 = CFrame.new(c.X*scale,c.Y*scale,c.Z*scale) * v.C1.Rotation
	end
end

I tried it with several big and small values, so hopefully it works for you when you try it.

Thank you so much for your help, but the problem I’m running into is that the models use WeldConstraints instead of Welds which don’t have C0/C1 values.

WeldConstraints are a lot easier to work with than Welds when crafting a model in studio, but I found out that animations which move the model (as opposed to only rotating parts) break the positioning of parts attached to WeldConstraints when the model’s size changes.

I think the solution I found is to just go back to the idea of programmatically converting all WeldConstraints to Welds; these models can have their size changed much more easily but I’ve been avoiding it since it seems hacky & has a lot of kinks to work out, but I think I’m finding that programmatically changing the size of an animated model with WeldConstraints is impossible

Thank you very much for your input though, I appreciate anyone who takes the time to help a forum post :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.