Typewriter Effect in text label Help

I have been trying multiple ways of making the text appear as a typewriter effect, but I wasn’t able. The text label I’m trying to do this in is inside a Frame which is inside another Frame inside a Screen GUI inside the Starter GUI folder.

This is just part of the code, but the missing things are irrelevant for what I'm trying to accomplish

local textButton = script.Parent.Frame.DialogueBox.TextButton
local frame = script.Parent.Frame
local dialogue = frame.DialogueBox.Dialogue 

local clickDetector = workspace.MarketRig.ClickDetector
local StarterGui = game:GetService("StarterGui")

local currentText = "1"

local function onButtonClicked()
	if currentText == "1" then
		dialogue.Text = "Do you remember anything previous to being here?"
		currentText = "2"	
		
	elseif
		currentText == "2" then
		dialogue.Text = "Well, i guess that doesnt matter, the only way out is trough that giant door"
		currentText = "3"

		
	elseif 
		currentText == "3" then
		dialogue.Text = "But I dont have the courage to venture out yet"
		currentText = "4"	
		
	elseif 
		currentText == "4" then
		dialogue.Text = "At least we are safe here. The SUN watched over us"
		currentText = "5"	
	
	elseif 
		currentText == "5" then

		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
		clickDetector.MaxActivationDistance = 32

		currentText ="1"
		dialogue.Text = "You must be the new guy, I havent seen you before."
		
	end
end

textButton.MouseButton1Click:Connect(onButtonClicked)

This is for dialogue, whenever the text button is clicked, the current text changes to the next one, and so on.

The text is a placeholder, pls dont bully :frowning:

1 Like

You can easily make a typewriter effect with this function, I’ve also optimized the way you do conditions a bit too.

local CurrentDialogue = 1
local Dialogues = {
	[1] = "Do you remember anything previous to being here?",
	[2] = "Well, i guess that doesnt matter, the only way out is trough that giant door",
	[3] = "But I dont have the courage to venture out yet",
	[4] = "At least we are safe here. The SUN watched over us",
	[5] = "You must be the new guy, I havent seen you before."
}
local function TypewriterMain(Textlabel:TextLabel?, Text:string?)
	if not Text then
		return
	end
	for i = 1, #Text do
		task.wait(0.05 -  math.random()*(0.021-0.01) + 0.01) -- This is just a random yield you can change it to anything.
		Textlabel.Text = Text:sub(1,i)
	end
end
local function ChooseDialogue()
	TypewriterMain(dialogue,Dialogues[CurrentDialogue])
	CurrentDialogue += math.clamp(CurrentDialogue,1,5)
	if CurrentDialogue == 5 then
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
		clickDetector.MaxActivationDistance = 32
        CurrentDialogue  = 1
	end
end
textButton.MouseButton1Click:Connect(ChooseDialogue)
3 Likes

It triggers the error Players.UrielgettaGeymer.PlayerGui.Dialogue1.Typewriter:81: missing argument #3 to ‘clamp’ (number expected) - Client - Typewriter:81

Line 81 would translate to this one CurrentDialogue += math.clamp(1,5)

Also, the text doesn’t change.

This is the full code I’m using with the addition.

--Variables
local textButton = script.Parent.Frame.DialogueBox.TextButton
local frame = script.Parent.Frame
local dialogue = frame.DialogueBox.Dialogue 
local blackBars = script.Parent.BlackBarFrame

local clickDetector = workspace.MarketRig.ClickDetector
local StarterGui = game:GetService("StarterGui")


local DialogueCam = game.Workspace:WaitForChild("DialogueCam")
local camera = game.Workspace.CurrentCamera


local player = game:GetService("Players").LocalPlayer
local Character  = player.Character or player.CharacterAdded:Wait()
local CameraInst = workspace.Camera

local DialogueCam = workspace.DialogueCam.CFrame


local currentText = "1"

--This happens when the Click Detector is triggered
local function onClickTriggered()
	repeat wait()
		CameraInst.CameraType = Enum.CameraType.Scriptable
	until CameraInst.CameraType == Enum.CameraType.Scriptable
	
	CameraInst.CFrame = DialogueCam
	frame.Visible = true
	blackBars.Visible = true
	
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	clickDetector.MaxActivationDistance = 0
	
	
	--Making the player invisible
	for i, part in ipairs(player.Character:GetDescendants())do
		if (part:IsA("BasePart") or part:IsA("Decal")) and part.Name ~="HumanoidRootPart" then
			part.Transparency = 1
		end
	end
	
end

---This happens when the "Next" Button is clicked 
local CurrentDialogue = 1
local Dialogues = {
	[1] = "You must be the new guy, I havent seen you before.",

	[2] = "Do you remember anything previous to being here?",

	[3] = "Well, i guess that doesnt matter, the only way out is trough that giant door",

	[4] = "But I dont have the courage to venture out yet",

	[5] = "At least we are safe here. The SUN watched over us",
	
	
}
local function TypewriterMain(Textlabel:TextLabel?, Text:string?)
	if not Text then
		return
	end
	
	for i = 1, #Text do
		task.wait(0.05 -  math.random()*(0.9 -0.01) + 0.01) -- This is just a random yield you can change it to anything.
		Textlabel.Text = Text:sub(1,i)
	end
end

local function ChooseDialogue()
	TypewriterMain(dialogue,Dialogues[CurrentDialogue])
	CurrentDialogue += math.clamp(1,5)
	if CurrentDialogue == 5 then
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
		clickDetector.MaxActivationDistance = 32
		frame.Visible = false
		blackBars.Visible = false	
	end
end


textButton.MouseButton1Click:Connect(ChooseDialogue)
clickDetector.MouseClick:Connect(onClickTriggered)

Try changing the ChooseDialogue function to this:

local function ChooseDialogue()
	TypewriterMain(dialogue,Dialogues[CurrentDialogue])
	CurrentDialogue += 1; CurrentDialogue = math.clamp(CurrentDialogue,1,5)
	if CurrentDialogue == 5 then
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
		clickDetector.MaxActivationDistance = 32
		frame.Visible = false
		blackBars.Visible = false	
	end
end
1 Like

I’ve edited the script to fix that error please re-paste it.

1 Like

Can’t you just set the text and then change the MaxVisibleGraphemes property for the same effect?

1 Like

The MaxVisibleGraphemes thing just changes the quantity of characters the text label can have, it doesn’t animate the text into appearing one by one.

Your reply is very contradicting. Have you tried using it?
Just keep adding onto it incrementally, from 0, until you reach the length of the text.

Well yeah, I guess that could work, but we like to complicate ourselves around here!
Also, I don’t know how to do it :stuck_out_tongue:

Yes, your solution also works.

dialogue.MaxVisibleGraphemes = 0

while true do
	dialogue.MaxVisibleGraphemes = dialogue.MaxVisibleGraphemes + 1
	wait(0.053)
end

1 Like

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