Unsure how to use math.clamp with a tween(udim2)

I want to tween a udim2 frame and add to it without exceeding the max, however I want to add a general number without having to calculate with division so that the frame will merely hit the max without exceeding. I’ve researched a bit however im not sure how to integrate it into my code.

I’ve come across a couple similar forums, however im almost positive that their solution will not work with the tween im using.

1 Like

bar.Size - UDim2.new(0, take, 0, 0) is bad, as bar.Size is constantly changing throughout the tween.

Would using a variable local barSizeBefore = bar.Size and instead doing barSizeBefore - UDim2.new(0, take, 0, 0) work?

If this doesn’t solve your issue, would appreciate a deeper explanation with what you’re trying to achieve

alright, i just edited my post. basically i want to tween the frame up to the max without an exact calculated number, but to where the frame doesnt exceed the max.

so if i were to make take 370 for instance, whilst the max is 360(on the frame x size), i want it to not exceed 360

Ahh I think I understand

bar.Size - UDim2.new(0,take,0,0)

bar.Size - UDim2.new(0,newbarsize,0,0)

i got it now thanks.

local barSizeBefore = bar.Size
			local clamped = bar:WaitForChild("Clamped").Value
			local min,max = 0,360
			local val = math.clamp(clamped, min, max)
			--max 360
				bar:TweenSize(
					barSizeBefore - UDim2.new(0, val, 0, 0),
					"Out",
					"Quad",
					.3,
					false
				)

i created a value(probably unnecessary) and clamped that value and used that to tween the bar