Refreshing Text Labels Problem

I currently have this code:

textbox.InputEnded:Connect(function()
			for i, v in pairs(whiteListTableDisplay) do
					print(i)
					posValue2 = posValue2 + 0.05 

					local newLabel = Instance.new('TextLabel')

					newLabel.Parent = scFrame
					newLabel.Position = UDim2.new(0.497, 0, posValue2 , 0) 
					newLabel.Size = UDim2.new(0.499, 0,0.031, 0)

					newLabel.FontFace.Bold = true
					newLabel.Text = tostring(v)
					newLabel.TextScaled = true
					newLabel.TextColor3 = Color3.new(1, 1, 1)				

			end
		end)

However this is not an ideal solution and keeps spamming new text labels when input begins, or ends again.
spm

How would I make it so when input stops and in the for loop each value will be “refreshed” and not cloned with a different position if it’s the same value

If its spamming labels, just remove the print()

doesnt change the fact it keeps creating labels

You are Using Instance.new(), Its going to create more, you are also using a loop, its going to constantly Create more and more without stopping.

I am aware, it’s only supposed to create/update if a new instance gets added to the table

Try:

local previousLabel = {}
textbox.InputEnded:Connect(function()
	for i, instance in ipairs(previousLabel) do
		instance:Destroy()
	end

	for i, v in pairs(whiteListTableDisplay) do
		
		posValue2 = posValue2 + 0.05 

		local newLabel = Instance.new('TextLabel')

		newLabel.Parent = scFrame
		newLabel.Position = UDim2.new(0.497, 0, posValue2 , 0) 
		newLabel.Size = UDim2.new(0.499, 0,0.031, 0)

		newLabel.FontFace.Bold = true
		newLabel.Text = tostring(v)
		newLabel.TextScaled = true
		newLabel.TextColor3 = Color3.new(1, 1, 1)		
		
		table.insert(previousLabel, newLabel)
	end
end)

NOTE: This is not efficient code, I have no idea what you are trying to do with this code, more details might make it better.

I believe all you need to do is:

table.remove(previousLabel)

Not destroy it.

No, the table stores all existing labels, the problem, I believe, is there are too many instances, so we need to delete them.

it just now has weird behavior like the first time i add something, it doesn’t create, then the 2nd time it creates, then the 3rd time it creates like 5 times

Can you be more specific, what is this code trying to achieve, what does each textLabel contain?

im trying to make a whitelist system, i dont want the instances to delete but when I remove that line is starts breaking and text labels are creating/not creating

Wait so like an autocorrect feature?

wdym

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

i tried a bunch of different ways to approach this but they all keep on randomly duplicating the text labels

What are you whitelisting, what is the point of the textLabels, I can only help you if you give me information.

This is something I had to bug fix when making a custom leaderboard.

Perhaps my old code could help here?

Hope this helps.

What does each entry represent?
Are these scores, so can be linked to a player?
The reason I ask, is because if you can have a key (either an index, or playerId, etc…) then you can just check to see if a text label with the same key exists, and if so, use that label, if not, create a new one.

Showcase of what im doing:

Problem: It keeps incrementing the other label positions instead of incrementing the new label

Code:

local previousLabel = {}
	textbox.InputEnded:Connect(function()
		for i, instance in ipairs(previousLabel) do
			instance:Destroy()
		end

		for i, v in pairs(whiteListTableDisplay) do

			posValue2 = posValue2 + 0.05 

			local newLabel = Instance.new('TextLabel')

			newLabel.Parent = scFrame
			newLabel.Position = UDim2.new(0.497, 0, posValue2 , 0) 
			newLabel.Size = UDim2.new(0.499, 0,0.031, 0)

			newLabel.FontFace.Bold = true
			newLabel.Text = tostring(v)
			newLabel.TextScaled = true
			newLabel.TextColor3 = Color3.new(1, 1, 1)		

			table.insert(previousLabel, newLabel)
		end
	end)

@SelDraken this is an example of what im doing, it’s not binded to anything

This issue I believe is caused by automatic size on the Scrolling frame, if this is the case, try changing the scrolling frame size whenever you add or remove a child to it.