Script is changing a table when it shouldn't

Hey forum (once again…) I am currently working on a function that sizes all text correctly without needing TextScaled, here is code:

function ReSize(UITable,Player)
	local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("")
	
	local OriginalTextSizes = {}
	
	for _,ui in pairs(UITable) do
		table.insert(OriginalTextSizes,ui)
	end
	
	remoteEvent:FireClient(Player)
	
	remoteEvent.OnServerEvent:Connect(function(FiredPlayer,ScreenResolution)
		if FiredPlayer == Player then
			for _,ui in pairs (UITable) do
				
				local OriginalInstance = table.find(OriginalTextSizes,ui)
				local OriginalTextSize = OriginalTextSizes[OriginalInstance].TextSize
				
				print(OriginalTextSize)
				
				if ScreenResolution.X >= 1500 and ScreenResolution.X <= 1999 then
					ui.TextSize = OriginalTextSize + 5
				elseif ScreenResolution.X >= 1000 and ScreenResolution.X <= 1499 then
					ui.TextSize = OriginalTextSize
				elseif ScreenResolution.X >= 500 and ScreenResolution.X <= 999 then 
					ui.TextSize = OriginalTextSize - 5
				elseif ScreenResolution.X >= 0 and ScreenResolution.X <= 499 then
					ui.TextSize = OriginalTextSize - 10
				end
			end
		end
	end)
end

The problem is when the TextSize changes OriginalTextSize Changes too, I thought maybe it’s because of:

 ui.TextSize = OriginalTextSize - 10 --or -5,+5 etc

but I think it shouldn’t change along with it

--I proved that it changes with this line 
print(OriginalTextSize)

Is there anything I can do?

1 Like

OriginalTextSize is a variable with the value OriginalTextSizes[OriginalInstance].TextSize.
OriginalTextSizes[OriginalInstance] is an Instance.

The problem you’re having is that when the TextSize property changes it’ll update TextSize in the Instance. You want to store the original text size as a number not the Instance itself.

for _,ui in pairs(UITable) do
    table.insert(OriginalTextSizes,ui.TextSize)
end

....

local OriginalTextSize = OriginalTextSizes[OriginalInstance]
2 Likes

I have thought of this but the problem with this is that I can’t find which one matches with which as I’m only storing the numbers.

--How the table looks with that code:
local OriginalTextSizes = {10,20,40,20} --Not the actual sizes

local OriginalTextSize = OriginalTextSizes[OriginalInstance] 
--I can't find the instance since only the numbers are stored
1 Like
for _,ui in pairs(UITable) do
    OriginalTextSizes[ui] = ui.TextSize
end

You can set the value using the Instance as the key.

2 Likes

Ah thank you! I didn’t know how to insert values in a table with a key either so this might not be just helpful for this :wink: I appreciate it!

2 Likes