Making a counter with text scaled

Text scaled is amazing and allows me to scale things perfectly with 0 math, although the issue I have with it is that any font other than code will have a different size based on changing characters.

How can I find a way to basically, get the font size once (and make it fit with all devices and string length, like textscale = true) but for it to stay that way when I modify the text. Basically if there’s some readonly value for textscaled that shows how big the text actually is.

Anyway here is a gif of the issue

Secondary issue is that with making it align in the center it also makes it do this ugly offset thing.

Hey 4slug!

Essentially what you’re asking is if there’s a way to set the font size to be non-changing after it was initially set to the proper size.

I think you might be over thinking this though, all you need to do it turn text scaled off and set the font size manually, then it will stay the same!

Hope this helped!

I need it to work on both phones and PC’s and the string lengths vary so I haven’t been able to find a manually set size that works well.

I see, so your issue is that you want the perfect size regardless of screen dimensions, but you also don’t want the constant shift in sizes.

Well as far as I know there’s no way to simply make the font size scaled and then not and keep the size that it was. You could test the device in a script and allow preset sizes per device, but that would be quite time consuming.

I’m sorry I couldn’t help more, have a nice night!

Thanks anyway, if only I could read the actual size of the text when it was auto scaled, this whole thing would be so much easier. The only solutions I can think of so far is to manually check each size if it fits and use the largest that fits.

You could technically calculate this value yourself, by using the TextFits property.

You could set the TextSize to 100, and use a loop to decrease it by 1 until TextFits equals true. This would give you the biggest size where the text still fits inside of the bounds of the TextLabel.

TextLabel.TextSize = 100
TextLabel.TextWrapped = false
FitSize = nil

while TextLabel.TextFits == false do
    TextLabel.TextSize -= 1
end
FitSize = TextLabel.TextSize

This isn’t too practical for multiple reasons, but it would work if this was your only option.

3 Likes

It’s actually what I ended up using as I couldn’t find anything else. I changed it a bit to be a little less sketchy but same general idea.

local LastSizes = {}

local function findText(label)
	for i = 100,1,-1 do
		label.TextSize = i
		if label.TextFits then
			return i
		end
	end
end

local function getSize(label, text)
	local width = tostring(workspace.Camera.ViewportSize.X)
	local index = text.." "..width
	
	if not LastSizes[index] then
		LastSizes[index] = findText(label)
	end
	
	return LastSizes[index]
	
end

local text = remotes[val].Value
local size = getSize(state, text)
state.TextSize = size
local pixels = (state.AbsoluteSize.X/2) - state.TextBounds.X/2
	
state.Position = UDim2.new(0, pixels, 0, 0)

In this case I set the text alignment to the left because if it was center it would still move, the last bit of code is to make it appear center

1 Like