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

How can you keep an object centered while using :TweenSize()?

So, everything works fine, but I don't know how to go around keeping the object centered where it was previously.

Code:

local b = script.Parent
local wait = 5

b.MouseButton1Down:connect(function()
	b:TweenSize(UDim2.new(0, 150, 0, 150), nil, "Linear", wait, nil)
end)

1 comment

1 answer

TweenSizeAndPosition

It's simple really. If you're increasing the size, change the position at the same time (By about half the amount you're increasing the size by).

Try this utility function for it.

function TweenFromCentre(obj, newdim, ...)
	local os, op = obj.Size, obj.Position;
	local dsx,dsy,dox,doy = newdim.Scale.X - os.Scale.X, newdim.Scale.Y - os.Scale.Y, newdim.Offset.X - os.Offset.X, newdim.Offset.Y - os.Offset.Y
	return obj:TweenSizeAndPosition(
		newdim, 
		UDim2.new(
			op.Offset.X - dox*.5,
			op.Scale.X - dsx*.5,
			op.Offset.Y - doy*.5,
			op.Scale.Y - dsy*.5
		),
		...
	);
end;

How does it work?

It changes the size, and gets the difference in size from the start of the tween to the end of the tween. Then it makes sure to move the position of the thing by half the difference to offset the change. The result is that the object changes size from the centre.

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