Help with resizing frame based on it's children

So right now, my frame only scales based on the text label for some reason.



Full Code:

local frame = script.Parent
local TextLabel = frame:WaitForChild("KillsLabel")
local ImageLabel = frame:WaitForChild("ImageLabel")
local BaseSize = TextLabel.Size.X

local RunService = game:GetService("RunService")
local TextService = game:GetService("TextService")

local function resizeFrameToChildren(frame)
	local maxWidth = 0

	for _, v in ipairs(frame:GetChildren()) do
		if v:IsA("GuiObject") then
			local childAbsoluteSize = v.AbsoluteSize
			maxWidth = math.max(maxWidth, childAbsoluteSize.X)
		end
	end

	frame.Size = UDim2.new(UDim.new(0, maxWidth), frame.Size.Y)
end

local function ScaleTextLabel(textlabel)
	local textSize = TextService:GetTextSize(textlabel.Text, textlabel.TextSize, textlabel.Font, Vector2.new(math.huge, math.huge))
	local textWidth = textSize.X

	local newSize = UDim2.new(0, textWidth, textlabel.Size.Y.Scale, textlabel.Size.Y.Offset)
	local newPosition = UDim2.new(1.75, -textWidth, textlabel.Position.Y.Scale, textlabel.Position.Y.Offset)

	if textWidth > BaseSize.Offset then
		textlabel.Size = newSize
		textlabel.Position = newPosition
	end
end

RunService.RenderStepped:Connect(function()
	ImageLabel.Position = UDim2.new(0, 20, ImageLabel.Position.Y.Scale, ImageLabel.Position.Y.Offset)
	
	resizeFrameToChildren(frame)
end)

TextLabel:GetPropertyChangedSignal("Text"):Connect(function()
	ScaleTextLabel(TextLabel)
end)

ScaleTextLabel(TextLabel)
1 Like

Center the text label and add like 5 spaces in the front to make space for the icon.

1 Like

There is still one problem with the text scaling.

local function ScaleTextLabel(textlabel)
	local textSize = TextService:GetTextSize(textlabel.Text, textlabel.TextSize, textlabel.Font, Vector2.new(math.huge, math.huge))
	local textWidth = textSize.X

	local newSize = UDim2.new(0, textWidth, textlabel.Size.Y.Scale, textlabel.Size.Y.Scale)
	local newPosition = UDim2.new(0.75, -textWidth * textlabel.Size.X.Scale, textlabel.Position.Y.Scale, 0)

	if textWidth > BaseSize.Offset then
		textlabel.Size = newSize
		textlabel.Position = newPosition
	end
end

image

image

1 Like