Text Sequence Generation from Images

I have this function that generates sections of text from a custom font (which is just uploaded images with a dictionary relating the image to the letter), that fills a Gui object: it functionally works, i.e., it creates the text section, then shows it in sequence just as it should and repeat, creating a new section, showing it in sequence etc etc.

My main problem is that every time there the text is created, there is a very noticeable lag spike.
I’m only testing it in Studio local server but I’ve made sure all the images are preloaded (and the coroutine is commented as I thought that would be responsible for it).

Is there anything that can be done about the lag spike on text section load?

function createText(P, Colour)
	--coroutine.resume(coroutine.create(function(Colour)
		local Strings = {
			"Thisisanamazinggame", "Nosenseismade", "Piisexactlythree", "Thecakeisalie"
		}
		local Line = 0
		local lineLetters = 0
		local Count
		local textTable = {}
		local Section = false
		while true do
			repeat
				local String = string.upper(Strings[math.random(1,#Strings)])
				print(String)
				for i in string.gmatch(String, "%u") do
					local numberOfLetters = #P:GetChildren()
					local Letter = Instance.new("ImageLabel", P)
					Letter.Name = i
					Letter.Size = UDim2.new(0.05, 0, 0.05, 0)
					Letter.Image = Prefix .. Ancient.ancientLetters[i]
					Letter.ImageColor3 = Colour
					Letter.BackgroundTransparency = 1
					Letter.Visible = false
					
					if lineLetters * 0.05 > 0.95 then
						if Line * 0.05 > 0.95 then
							Line = 0
							lineLetters = 0
							Section = true
							print("End of Section")
							break
						end
						Line = Line + 1
						lineLetters = 0
					end
					
					Letter.Position = UDim2.new(lineLetters * 0.05, 0, Line * 0.05, 0)
					table.insert(textTable, Letter)
					lineLetters = lineLetters + 1
				end
			until Section == true
			for i, letter in next, textTable do
				letter.Visible = true
				wait()
			end
			wait(1)
			textTable = {}
			for i,v in next, P:GetChildren() do
				v:Destroy()
			end
			Section = false
		end
	--end),Colour)
	print("TextCreated")
end

Many thanks.

1 Like

i dont know how much this will help, but dont use the second argument of Instance.new()

Another way to speed up the time it takes to create each ImageLabel is to create a template object which is cloned for each letter.

You should also consider using a sprite sheet so that the assets don’t have to load individually.

Even with using all the insights given above, the lag-spike still occurs, the best solution I’ve come up with, is to just set the text once, and just have it repeat that text, rather than have it generate a different section of text each sequence. I’m more or less happy with this.

Many thanks for your help.