I need some help understanding this

Note: idk if this is the right category

I am trying to understand this code here

 local player = game.Players.LocalPlayer
local CreateDialogueEvent = game.ReplicatedStorage.CreateDialogueEvent
local DialogueFrame = player.PlayerGui.ScreenGui.Frame

local function playSound(sound_id) -- Plays typing sound
	local sound = Instance.new("Sound",game.ReplicatedStorage)
	sound.SoundId = sound_id
	sound.Volume = 1
	sound.PlayOnRemove = true
	sound:Destroy()
end

local function textAnimate(content) -- Animates each letter
	for i=1,string.len(content) do -- why did they use content
		DialogueFrame.TextLabel.Text = string.sub(content,1,i) -- what is string.Sub?
		playSound("rbxassetid://915576050")
		if string.sub(content,i,i) == "!" or string.sub(content,i,i) == "." or string.sub(content,i,i) == "?" then
			wait(1)
		elseif 	string.sub(content,i,i) == "," then
			wait(.5)
		else
			wait(.05)
		end
	end
end

CreateDialogueEvent.OnClientEvent:Connect(function(image_id, content) -- activates when called from the Server
	if not player:findFirstChild("secretEnding") then
		DialogueFrame.ImageLabel.Image = image_id
		DialogueFrame.TextLabel.Text = ""
		DialogueFrame.Visible = true
		textAnimate(content)
	end
end)

it is a local script in starterCharacter scripts

The part I don’t understand is when they say string.Sub, I looked at the dev forum and it I still didin’t understand it, I am also confused why on the start they used the content in the for i loop

this script animates the text
I will comment the parts I don’t understand

They use the content argument because it is the text they should animate, not sure why string.len was used when # can be used

string.sub basically gets a part of a string

local text = "This is text"

local textSub = string.sub(text, 1, 6) --Gets the first 6 letters

print(textSub) --Prints This i

It’s basically just used in that case for a typewriter effect and for getting specific characters to check if they’re equal to certain characters for the waiting part

content is the parameter of the textAnimate function. When it mentions content it is most likely referring to the string to animate.

string.sub is used to get specific letters of a string. For example, let’s say I wanted to get letter 3 of the word “Hello”, I could do print(string.sub("Hello",3,3)), and it would print out the l.

In this case, they are looping through each letter of the string to animate it.

The # symbol for length is only used with arrays, so when finding the length of a string, you need to use string.len().

# works with strings as well

local text = "This is text"

print(#text) --Prints 12

image

1 Like

Oh, I never knew that :slight_smile:, thanks for pointing that out!

I personally prefer string.len() because it looks more organized in my code.

1 Like

hmmm ok, but what is the

string.sub(content,1,i)

like yeah I understand what the string.Sub does now, but why is there a (content, 1, i)

what does each one stand for?

It’s basically saying,

Give me a portion of the content string from 1 (the beginning) to what number i is

Say the loop is at the 3rd iteration, it would be seen as

string.sub(content, 1, 3)

So it gets the first 3 characters

Say you have this

local text = "Hello"

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

It would slowly print out the full string 1 character at a time, so the expected output would be this

H
He
Hel
Hell
Hello

String.Sub(The text, 1, #text) – this will get all of the letters in the text that we have

is this correct?

if so then

for i = 1, #Text do
print(String.Sub(The text,1,#text)
end

Yep, so if you have

local text = "This is text"

print(string.sub(text,1,#text))

It would basically act as you just printed the ext only

And no, in your case if you want it to go one letter at a time, you need to use i instead of #text if it’s a for i loop

ohh ok, but then what would “i” stand for?

The i is just the variable that stores the int to end the substring. Since it’s a loop, i is just whatever the iteration of the loop is.

i would be the index/iterator, aka that variable you name in a for loop

Let’s go back tothis

local text = "Hello"

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

There we have i, a for loop is basically a incremental loop. Here it’s saying to go from 1 to 5, since the stirng we have is 5 charactrs long. So it starts at 1, and prints the portion from the first character to 1, then it’s at 2 now, so it gets the first two characters and so on

ohhh, ok that makes much more sense :slight_smile:

but then why did then use (content,i, i) here

		if string.sub(content,i,i) == "!" or string.sub(content,i,i) == "." or string.sub(content,i,i) == "?" then

That’s for getting a single character only

Example

local text = "Hello"

print(string.sub(text,3,3)) --Prints l

In this case, it’s getting a single character and comparing it. So if the character is ! . or ? it will do something

ohhh, ok

Thank you BmbatTheHybrid, this is very helpful :slight_smile:

1 Like