Help with Replacing a string by letter

I want to replace a randomly generated Giberish text with an actual message a letter at a time to give an illusion of the program “Sorting” the text out into an sentence
I might be bad at explaining here what I meant

When the funtion call create a giberish with the same letter amount as the actual message

When I call another funtion the actual message will start replacing the giberish letter by letter

Until the Full Sentense appear

Here is a test place you can take a look at what I’m doing right now
thing relate to the topic are inside StarterGui
(Please don’t mind other silly thing, I’m just a bored dev)
Test a lot of thing.rbxl (76.6 KB)

well firstly you need a characters list
for ex.

local chars = string.split("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM", "")

then, on a loop you want to update the text

local textToShow = "This is a dialogue system test"

while task.wait(.1) do
local newText = ""
for i = 1, #textToShow do
newText ..= chars[math.random(1,#chars)]
end

textLabel.Text = newText
end

Then we gradually increase a number that will determine the ammount of correct letters to show

local chars = string.split("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM", "")
local num = 0

local textToShow = "This is a dialogue system test"

while task.wait(.1) do
	num += 1

	local newText = string.sub(textToShow, 0, num)
	for i = num+1, #textToShow do
		newText ..= chars[math.random(1,#chars)]
	end

	print(newText) -- edit this

	if num >= #textToShow then
		break
	end
end

result:
image

1 Like

the script for those who want to recreate something like this

how to do it

  1. Create a remoteEvent inside ReplicatedStorage then name it “CatchPlrMessage”

  2. put UIScript like this
    image

  3. Put ServerScript inside ServerScriptService

  4. You done

GUI script (localScript)
local TweenService = game:GetService("TweenService")

local Msg = " "
local Text = script.Parent.Frame.TextLabel

local function GetTotalLetterInMessage(message : string)
	return #message
end

local function CreateGiberish()
	local Giberish = ""
	for i = 1, GetTotalLetterInMessage(Msg) do
		local RandomNumber = math.random(1, 2)
		if RandomNumber == 1 then
			Giberish = Giberish .. string.upper(string.char(math.random(97, 122)))
		else
			Giberish = Giberish .. string.char(math.random(97, 122))
		end
	end
	return Giberish
end

local function Replacing()
	local TextToReplace = Text.Text
	if string.len(TextToReplace) <= 0 then
		warn("You can't replace empty text!")
		return
	end
	Text.Parent.Visible = true
	local chars = string.split(TextToReplace, "")
	local num = 0

	while task.wait(0.03) do
		num += 1

		local newText = string.sub(Msg, 0, num)
		for i = num+1, #Msg do
			newText ..= chars[math.random(1,#chars)]
		end

		Text.Text = newText

		if num >= GetTotalLetterInMessage(Msg) then
			break
		end
	end
end

game:GetService("ReplicatedStorage").CatchPlrMessage.OnClientEvent:Connect(function(msg)
	Msg = msg
	Text.Parent.Visible = true
	Text.Text =  CreateGiberish()
	Text.MaxVisibleGraphemes = 0
	
	local tween = TweenService:Create(Text, TweenInfo.new(string.len(msg)/50), {MaxVisibleGraphemes = utf8.len(Text.ContentText)})
	tween:Play()
	tween.Completed:Wait()
	Replacing()
	Text.MaxVisibleGraphemes = -1
end)
ServerScript for detecting player chat message
local plr = game:GetService("Players")

plr.PlayerAdded:Connect(function(Player)

	Player.Chatted:Connect(function(msg)

		game:GetService("ReplicatedStorage").CatchPlrMessage:FireClient(Player, msg)
		
	end)

end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.