TextSize gets bigger on CharacterAdded

Basically, I have a script that I’ve been using for some time, what it does is it keeps the TextSize property constant for (hopefully) all screens, however I’ve just noticed that when CharacterAdded happens (you are given a new character) the property increases.

Script:

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

-- this part just gets what's on StarterGui, most likely not the problem
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
                -- the problem (most likely)
				local NewTextSize = (OriginalTextSizes[v] * reduction) * increasement
				v.TextSize = NewTextSize
			end
		end
	end
end

while true do
	ResizeText()
	wait(0.2)
end

I have tried printing “OriginalTextSizes[v]”, “reduction”, “increasement” and they all stay the same so I’m a bit lost

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

– this part just gets what’s on StarterGui, most likely not the problem
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] = 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
            -- the problem (most likely)
			local NewTextSize = (OriginalTextSizes[v] * reduction) * increasement
			v.TextSize = NewTextSize
		end
	end
end

end

while true do
ResizeText()
wait(0.2)
end

try this.

1 Like

It gives the error “attempt to perform arithmetic (add) on nil and number” on this line(12):

OriginalTextSizes[v] = OriginalTextSizes[v] + v.TextSize

use this instead OriginalTextSizes[#OriginalTextSizes+1] = v.TextSize

if this doesnt work use this

OriginalTextSizes[#OriginalTextSizes+1] = {v.TextSize}

That makes it so it’s impossible to identify which TextSize goes to each TextUi:

{[1] = textsize}
--1 being the representation of #OriginalTextSizes+1

Turns out it was a Starter/Player Gui thing, parented it to StarterPlayerScripts and did some modifications and now it works