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.
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)
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)
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