Tryding to make typing type dialog, not printing anything

so I have been trying to make a typing type dialog system for my game, but my code isn’t working. I am new to this and I don’t even know if my logic is correct, but it’s not printing anything into the TextLabel

code:

function npcTalk(message)
	local text = playergui.DialogueGui.Frame1.Dialogue.Text
	local messageNum = -string.len(message) + 1
	repeat
		text = string.sub(message, 1, messageNum)
		messageNum = messageNum + 1
	until(messageNum == 0)
end
2 Likes

Have you heard of textbox? TextBox | Documentation - Roblox Creator Hub

by “typing type dialog system” I mean that instead of just changing the text, it has a transition
the npc would do domething like:
e ex exa exam examp exampl example
except that the previous one would be replaced

Is this like a typewriter effect?

You could do something like this:

local function typewrite(message)
    for i = 1, #message, 1 do
        TextLabel.Text = string.sub(message, 1, i)
        task.wait(0.05)
    end
end

Edit: This was edited after the updated version below.

Yeah I know, you can use tweenservice to do this. Or @12345koip method.

Text is not a tweenable property. I’ve tried it myself.

I do not know, i can try it
extra text

1 Like

Try his method I think it works.

I just tried it and it didn’t work for me. it prints the text but not in the typing kind of fashion i would like, the text just shows up

My bad, I forgot a wait time. Try this:

local function typewrite(message)
    for i = 1, #message, 1 do
        TextLabel.Text = string.sub(message, 1, i)
        task.wait(0.05)
    end
end

this works well, but why?
how does this code work?

In this procedure, we are iterating over the length of the message.

string.sub returns the part of the given string within the two numbers. By using string.sub here, we can cut part of the message out. And since i is incremented each time it loops, it essentially means we get one more character from the string each time. Then, we set that as the text, and wait a little so the effect actually shows.

Let me know if you have any more questions.

how does this line:

 for i = 1, #message, 1 do

increment i?
please bare with me, i’m new

This is known as an iteration. It’s a fancy way of saying a loop. We give 3 things to a loop of this type:

  • The start number (start)
  • The end position (stop)
  • How many times it should go up by each time (step)

So, if we do this:

for i = 1, 10, 1 do
    print(i)
end

we will get this:

1
2
3
4
5
6
7
8
9
10

This is because each time we loop round, the current loop count is stored in the variable i. So, we want to get the length of our message. We can do this by using the hashtag.

print(#"hi there") --> outputs 8

So, we get our length and put it in the loop!

for i = 1, #"hi there", 1 do
    print(i)
end

The only difference before is that we used a variable to store our message beforehand. A variable is a pointer to a location in the memory that can change throughout the runtime of a program. Basically, it holds a value and you can change what value it holds whenever you want to.

Incremented means to increase.

We then use our string.sub function to get the snippet of the string between 1 and i, and since i is incremented by 1 each loop, we get +1 character from the string each loop!

local hello = "hello"

for i = 1, #hello, 1 do
    print(string.sub(hello, 1, i))
end

This would output:

h
he
hel
hell
hello

So, in short, we loop over the string, get a snippet from it, and put that on the text.

There are other types of iteration (loops), but I won’t talk about those here.

Sorry, this is a bit long so it might get a bit confusing, hope it helps :smiley:

1 Like

i have done other scripting languages before so this is pretty easy to understand, I just needed someone to teach me. so thank you!

1 Like

Nice! Sorry I made it really, really basic, I just didn’t know how good you were.

Glad I could help!

By the way I messaged you that gui.Visible thingy, did it work?

No reason to apologize, when teaching someone you should always make it as simple as possible unless the person you are teaching has shown you a reason not to

1 Like

Yes it did, thank you for that

No problem. Good luck on your game. And wow, you know html, css, and java? I only know luau and a bit of python, that’s impressive.