How to make this text effect effectively and efficiently? Having spaces and replacing certain random spaces with characters associated with string

I’m wanting to make a cool text effect where I have a whole line of spaces for text and then go about replacing randomly, characters (and also keep capitalization) associated with the string (stored in a different variable) and in the proper order so for example:

Stored string == "This is a Short Text Label."

String Spaces == "                           " --amount of spaces == stored string

and then go through a cycle to randomly give the effect of popping up characters over time:

“T is s a h xt abel” (ignore the space format error, dev forums auto format but you should get the idea)

I took a shot at attemping and let me just say it was not pretty, honestly terrible I wouldn’t recommend looking at it, I should also mention getting nil errors with the bellow method–nevermind that it never seemed to work. If anyone can help that would be great

local StringBellow = "This is a short textlabel."
		
		local ArrayOfChars={"T", "h", "i","s", " ", "i","s", " ", "a", " ", "s", "h", "o", "r", "t", " ", "t","e","x","t","l","a","b","e","l", "." }
		
		local BellowString = "                           "
		
		local counter=0
		coroutine.wrap(function()
			
		while counter~=27 do --amount of spaces
			local ChosenNum1
			local ChosenNum2
			local ChosenNum3
			local ChosenNum4
			local ChosenNum5
			repeat
					ChosenNum1 = math.random(1,#BellowString)
					print(ChosenNum1)
					if ArrayOfChars[ChosenNum1]==" " then
						table.remove(ArrayOfChars,ChosenNum1)
					end
					
				until ArrayOfChars[ChosenNum1]~=" "  and ArrayOfChars[ChosenNum1]~= nil
				counter+=1
				string.gsub(BellowString, ChosenNum1, ArrayOfChars[ChosenNum1])
				script.Parent.CriminalObjective.CrimObjectiveLabel.Text=BellowString
				table.remove(ArrayOfChars,ChosenNum1)
			wait(.05)
				repeat
					ChosenNum2 = math.random(1,#BellowString)
					
					if ArrayOfChars[ChosenNum2]==" " then
						table.remove(ArrayOfChars,ChosenNum2)
					end
					
				
					
				until ArrayOfChars[ChosenNum2]~=" " and ArrayOfChars[ChosenNum2]~= nil
				string.gsub(BellowString, ChosenNum2, ArrayOfChars[ChosenNum2])
				script.Parent.CriminalObjective.CrimObjectiveLabel.Text=BellowString
				table.remove(ArrayOfChars,ChosenNum2)
               if counter==27 then
					break
				end
			wait(.05)
			repeat
				ChosenNum3 = math.random(1,#BellowString)
					if ArrayOfChars[ChosenNum3]==" " then
						table.remove(ArrayOfChars,ChosenNum3)
					end
					
				until ArrayOfChars[ChosenNum3]~=" " and ArrayOfChars[ChosenNum3]~= nil
				counter+=1
				string.gsub(BellowString, ChosenNum3, ArrayOfChars[ChosenNum3])
				script.Parent.CriminalObjective.CrimObjectiveLabel.Text=BellowString
				table.remove(ArrayOfChars,ChosenNum3)
				
			wait(.05)
			repeat
				ChosenNum4 = math.random(1,#BellowString)
					if ArrayOfChars[ChosenNum4]==" " then
						table.remove(ArrayOfChars,ChosenNum4)
					end
					
				until ArrayOfChars[ChosenNum4]~=" " and ArrayOfChars[ChosenNum3]~= nil
				counter+=1
				string.gsub(BellowString, ChosenNum4, ArrayOfChars[ChosenNum4])
				script.Parent.CriminalObjective.CrimObjectiveLabel.Text=BellowString
				table.remove(ArrayOfChars,ChosenNum4)
			wait(.05)
			repeat
				ChosenNum5 = math.random(1,#BellowString)
					if ArrayOfChars[ChosenNum5]==" " then
						table.remove(ArrayOfChars,ChosenNum5)
					end
				until ArrayOfChars[ChosenNum5]~=" " and ArrayOfChars[ChosenNum5]~= nil
				counter+=1
				string.gsub(BellowString, ChosenNum5, ArrayOfChars[ChosenNum5])
				script.Parent.CriminalObjective.CrimObjectiveLabel.Text=BellowString
				table.remove(ArrayOfChars,ChosenNum5)
			wait(.05)
			
			end
		end)()

Idk if this helps but I was able to write this:

function showString(str)
	local spaceChar = " "
	local iterations = 8
	local chance = 1/4

	local chrs = table.create(#str, spaceChar)
	local rand = Random.new()

	local function addLetters(numLetters)
		local lettersAdded = 0
		while lettersAdded < numLetters and table.concat(chrs):gsub(spaceChar, " ") ~= str do
			for i = 1, #str do
				local char = str:sub(i, i)
				if chrs[i] == char or rand:NextNumber() > chance then
					continue
				end
				
				chrs[i] = char
				lettersAdded += 1
				if lettersAdded == numLetters then
					break
				end
			end
		end	
	end
	
	local fx = {table.concat(chrs)}
	while table.concat(chrs):gsub(spaceChar, " ") ~= str do
		addLetters(math.floor(#str/iterations + 0.5))
		table.insert(fx, table.concat(chrs))
	end
	
	return fx
end
print(showString("This is a Short Text Label."))
--[[ OUTPUT:
 {
    [1] = "                           ",
    [2] = " h   is                    ",
    [3] = " h s is     o t            ",
    [4] = "Th s is     o t  e t       ",
    [5] = "Th s is     ort Text       ",
    [6] = "Th s is   Short Text      .",
    [7] = "Th s is a Short Text  ab  .",
    [8] = "This is a Short Text Labe .",
    [9] = "This is a Short Text Label."
}
]]
1 Like

really cool and easily customizable. Thanks for your help.

1 Like