Adding onto another string

So recently I was trying to make a dialogue system for my game. And I wanted it to be so that text can build up on each other using a typewriter function.
For example:
Dialogue: Dev forum
waits 3 sek
Dialogue2: Dev forum (< already existent text that doesn’t have to be rewritten by typewriter) is cool.

My typewriter function:

function dialouge.TypeWriter(text, lenght, narrator)
	local narratorSound = narrator.Sound
	
	for i = 1, #text, 1 do
		textBox.Text = string.sub(text, 1, i)
		narratorSound:Play() 
		wait(lenght)
	end
end

I tried doing something like this:

function dialouge.TypeWriter(text, lenght, narrator)
	local narratorSound = narrator.Sound
	
	for i = 1, #text, 1 do
		textBox.Text = textBox.Text.." "..string.sub(text, 1, i)
		narratorSound:Play() 
		wait(lenght)
	end
end

But it kinda broke it really hard.

Any ideas?

When you execute currently, what happens?

1 Like

Currently it looks like this:
I got no video sorry.

The thing I wanna say: “Roblox | devforum is | cool” – “|” stands for a part of dialogue
It will do this:
Roblox (clears the text fully)
devforum is (clears the text fully)
cool


that’s how one dialogue thingy looks (ignore the text it’s just some stupid testing).

With the solution which failed it would look like this:
The thing I wanna say: “Roblox | devforum is | cool” – “|” stands for a part of dialogue
RobloxRobloxRobloxRobloxRobloxRoblox

It copies the text for each character which makes sense

use your first function and use string.split()

function Dialouge.TypeWriter(Text, Duration, Narrator)
  local NarratorSound = Narrator and Narrator.Sound
	
  for i = 1, #Text do
    if NarratorSound then NarratorSound:Play() end
    TextBox.Text = Text:sub(1, i)
    wait(Duration)
  end
end
local TestText = "Roblox|Devforum is|cool"
local Messages = TestText:split("|")

for _, Message in Messages do
  TextBox.Text = ""
  Dialouge.TypeWriter(Message, 0.05)
  wait(3)
end

there is an even bigger SIGMA coming in!!! :scream:

1 Like

replace your typewriter function:

function dialogue.TypeWriter(text, length, narrator, append)
    local narratorSound = narrator.Sound
    local startIdx = 1

    -- If append is true, start from the end of the current text and append the new text
    if append then
        startIdx = textBox.Text:len() + 1
        text = textBox.Text .. " " .. text
    end

    for i = startIdx, #text, 1 do
        textBox.Text = string.sub(text, 1, i)
        narratorSound:Play()
        wait(length)
    end
end

or something like:

function dialogue.TypeWriter(text, length, narrator, append)
    local narratorSound = narrator.Sound
    local currentText = textBox.Text -- Store the current text

    -- If append is true, keep the current text and append new text
    if append then
        text = currentText .. " " .. text
    end

    for i = #currentText + 1, #text, 1 do
        textBox.Text = string.sub(text, 1, i)
        narratorSound:Play()
        wait(length)
    end
end

To use this function:

  • Call dialogue.TypeWriter("Roblox", length, narrator, false) for the first part of the dialogue.
  • Call dialogue.TypeWriter("devforum is", length, narrator, true) to append the second part.
  • Call dialogue.TypeWriter("cool", length, narrator, true) to append the third part.

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