How would I integrate a size limiter for a drag-to-resize frame

Hi everybody,

For a while now I have had this problem where studio crashes because of horrible scripting (but I’ve been too lazy to try and figure it out). Basically I have a frame that you can drag to resize, and a size limiter. But due to the size limiter being in another script and being scripted poorly, it crashes studio.

I really just want to integrate the size limiter into the resize script (as the title says) so studio doesn’t crash. I have tried a few things including adding a debounce to the size limiter, but that just broke everything. The scripts are below.

Drag to resize script

Script

Size limiter script

Script

So that is basically it. Any pointers on how to do this would be great, thank you!

Edit: why did nobody tell me math.clamp exists??

~Robyn

i really recommend you to recreate your entire script and learn about TweenService, but here is the solution to the size limiter script:

local cam = workspace.Camera
local viewportSize = cam.ViewportSize

local MinSizeX = 484 --378
local MinSizeY = 347--271

--local MaxSizeX = 968
--local MaxSizeY = 694



local function ScaleToOffsetX(x)

	x *= viewportSize.X

	return x
end

local function ScaleToOffsetY(y)

	y *= viewportSize.Y

	return y
end

local MaxSizeX = ScaleToOffsetX(1)
local MaxSizeY = ScaleToOffsetY(1)

local ObjectSize = script.Parent

ObjectSize:GetPropertyChangedSignal("Size"):Connect(function()
	if ObjectSize.Size.X.Offset < MinSizeX then
		ObjectSize.Size = UDim2.new(ObjectSize.Size.X.Scale, MinSizeX, ObjectSize.Size.Y.Scale, ObjectSize.Size.Y.Offset)	
	end
	
	if ObjectSize.Size.Y.Offset < MinSizeY then
		ObjectSize.Size = UDim2.new(ObjectSize.Size.X.Scale, ObjectSize.Size.X.Offset, ObjectSize.Size.Y.Scale, MinSizeY)
	end
	
	if ObjectSize.Size.X.Offset > MaxSizeX then
		ObjectSize.Size = UDim2.new(ObjectSize.Size.X.Scale, MaxSizeX, ObjectSize.Size.Y.Scale, ObjectSize.Size.Y.Offset)	
	end

	if ObjectSize.Size.Y.Offset > MaxSizeY then
		ObjectSize.Size = UDim2.new(ObjectSize.Size.X.Scale, ObjectSize.Size.X.Offset, ObjectSize.Size.Y.Scale, MaxSizeY)
	end
end)
1 Like

You can use math.clamp, which could also reduce the lines in your script by alot.

Example,

local minX = 1
local maxX = 10

local minY = 1
local maxY = 10

local size = UDim2.new(0,math.clamp(X,minX,maxX),0,math.clamp(Y,minY,maxY))
2 Likes

I haven’t used TweenService in ages! What would TweenService be good for?

I had no clue that existed. Thank you, kind stranger!

1 Like

i recommend you to search to understand, because it can make so much cool things, like go from 1 to 10 in how many seconds do you want and with the interpolation that you want

Oh, I do know that. I was wondering if you meant it had anything to do with the script. Thank you anyways!

too lazy

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