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