Constant TextSize Property For GUI

Hey DevForum! I have just made a script to keep the property “TextSize” of TextLabels, TextBoxes, etc, constant for every screensize.


Instructions:

  • Place the script given below in a LocalScript
  • Place the LocalScript inside StarterGui
  • All set!

The script
local player = game.Players.LocalPlayer
local StarterGui = game.StarterGui:GetDescendants()
local Dictionary = {}
local OriginalTextSizes = {}

for i,v in pairs(StarterGui) do
	Dictionary[v.Name] = true
end

for i, v in pairs(script.Parent:GetDescendants()) do
	if v:IsA("TextLabel") or v:IsA("TextButton") or v:IsA("TextBox") then
		if Dictionary[v.Name] ~= nil then
			OriginalTextSizes[v] = v.TextSize
		end
	end
end


local function ResizeText()
	local x = workspace.CurrentCamera.ViewportSize.X
	local reduction = x / 1000
	local increasement = 1
	
	if x < 1000 and x >= 750 then
		increasement = 1.1
	elseif x < 750 and x >= 450 then
		increasement = 1.25
	elseif x < 450 then
		increasement = 1.5
	end
	
	for i, v in pairs(script.Parent:GetDescendants()) do
		if v:IsA("TextLabel") or v:IsA("TextButton") or v:IsA("TextBox") then
			if OriginalTextSizes[v] ~= nil then
				local NewTextSize = (OriginalTextSizes[v] * reduction) * increasement
				v.TextSize = NewTextSize
			end
		end
	end
end

while true do
	ResizeText()
	wait(0.2)
end

Warnings:

  • This isn’t meant to be a perfect script, just a quick fix to keep the sizes the same on every screensize so don’t expect it to keep the size precisely the same.
  • The script works based off of the X value of the screen resolution, if the player edits the Y it will most likely mess up the size

If you have any tips make sure to tell me!

Hopefully you find it useful :wink:

4 Likes

You could just use .TextScaled, or TextService GetTextSize

1 Like

.TextScaled can be an alternative to this but sometimes I prefer to keep every phrase/word the same size. This can be used for a cleaner typewriter effect, aesthetic purposes and more stuff

With TextSize:

With TextScaled (the going out of the box isn’t what I mean by a difference, it’s the size difference):

I haven’t heard about GetTextSize I’ll look into it!