How do I limit frame size?

Hello guys,
Recently I came across an issue while creating my level reward system.

When the player’s level reaches over the mission’s specific level, the frame will go over the intended stopping destination.

Here’s a video of it.

Code for it

local replicatedStorage = game:GetService("ReplicatedStorage")
local UpdateLevelUI = replicatedStorage:WaitForChild("UpdateLevelUI")

local debounce = false

UpdateLevelUI.OnServerEvent:Connect(function(player) 
	-- Player stats
	local playerLevel = player:WaitForChild("Values").Level
	local playerLevelFolder = player:WaitForChild("Missions")

	-- Frame
	local levelGUI = player.PlayerGui.Level
	local rewardFrame = levelGUI.Level.Rewards

	-- Find each mission
	for _, v in ipairs(rewardFrame:GetChildren()) do
		if not v:IsA("UIListLayout") then
			for _, x in ipairs(v:GetChildren()) do
				local progressBackground = x:FindFirstChild("ProgressBackground")
				local progress = progressBackground:FindFirstChild("Progress")

				local missionTitle = x:FindFirstChild("MissionTitle")
				local titleScreen = x:FindFirstChild("TitleScreen")
				
				titleScreen.Text = x:FindFirstChild("Title").Value
				
				missionTitle.Text = "Be level "..x:FindFirstChild("Level").Value
				
				progress:TweenSize(UDim2.new(playerLevel.Value / x:FindFirstChild("Level").Value, 0, 1, 0))
				
				x:FindFirstChild("Claim").MouseButton1Down:Connect(function()
					if not debounce then
						debounce=true
						if x:FindFirstChild("Level") then
							if playerLevel.Value >= x:FindFirstChild("Level").Value then
								if not playerLevelFolder:FindFirstChild("Level "..x:FindFirstChild("Level").Value).Value then
									print("Level reward given")
									playerLevelFolder:FindFirstChild("Level "..x:FindFirstChild("Level").Value).Value = true
								else
									warn("Already claimed!")
								end
							else
								warn("Not enough level to claim this reward!")
							end
						end
						task.wait(1)
						debounce = false
					end
				end)
			end
		end
	end
end)
3 Likes

if you use math.clamp it stops a number from going over or under a set limit

progress:TweenSize(UDim2.new(math.clamp(playerLevel.Value / x:FindFirstChild("Level").Value,0,1), 0, 1, 0))
2 Likes

Hello,
Thank you for the suggestion but unfortunately it outputs this when I implemented that code

ServerScriptService.RemoteEvents:29: invalid argument #1 to 'clamp' (number expected, got UDim2) 
1 Like
local Percentage = math.clamp(playerLevel.Value / x:FindFirstChild("Level").Value, 0, 1)
UDim2.fromScale(Percentage, 1)
1 Like

Thank you for your suggestion but it is still not working. I have never ever done this before so I don’t know how to stop it from overflowing.

1 Like

You can use math.clamp! This limits a number into a specified minimum and maximum. For this case, we will limit playerLevel.Value / x:FindFirstChild("Level").Value (the number) into minimum of 0 and maximum of 1 (so it doesn’t overflow).

-- replace line 29 with the line below
progress:TweenSize(UDim2.fromScale(math.clamp(playerLevel.Value / x:FindFirstChild("Level").Value, 0, 1), 1))
1 Like

Hello there,
Thank you for the suggestion. It is working as of now.

I have read on the Roblox creation page about it. Basically, Udim2.fromScale() has 2 parameters xScale and yScale. Normal UDim2.new() will take in 4 parameters. xScale, Xoffset, yScale and yOffset. As for the math.clamp part, it will take in 3 parameters. It will constraint the first number.

Which is not a bad idea.

2 Likes

Try enabling ClipDescendants in the container bar frame

1 Like

Hello Rafa0874,
May I ask what does ClipDescendants do?

I found this on the website.

When set to true, portions of GuiObjects that fall outside of the BillboardGui’s canvas borders will not be drawn. Even when this property is false, objects that are completely outside of the canvas of the BillboardGui will not render.

What is the difference xD

ClipDescendants is a property inside of the frames, every children inside of the frame will clip.

The API can teach more: GuiObject | Documentação - Central de Criadores Roblox

my bad, I did implement wrong.
(i edited it but it seems you have already fixed the issue)