What are you attempting to achieve? - Hello, I am attempting to achieve a system where you have a GUI, and I have a text table and it should follow the order of text in the table to replicate onto the GUI. Like in the game Roses, where in the beginning there is a text subtitle and it follows a particular order… I have tried doing a method but it isn’t working for me.
What is the issue? - I cannot get it to work
What solutions have you tried so far? - I tried a solution where you do something like this
local text = {
["Story1"] = "I have been trapped in this wasteland for over 5 years...",
["Story2"] = "Text here blah blah",
["Story3"] = "Another story here"
}
for i = 1, #text do
script.Parent.StoryTeller.Text = string.sub(1, i)
wait()
end
^^^ This is from a tutorial by SteadyOn but I tweaked it a little bit but yeah it doesn’t work.
local text = {
["Story1"] = "I have been trapped in this wasteland for over 5 years...",
["Story2"] = "Text here blah blah",
["Story3"] = "Another story here"
}
local Story = 1
for i = 1, #text[Story] do
script.Parent.StoryTeller.Text = string.sub(1, i)
wait()
end
You have to access the actual story key to get the string. You were also trying to substring, but didn’t specify the actual story text.
local text = {
["Story1"] = "I have been trapped in this wasteland for over 5 years...",
["Story2"] = "Text here blah blah",
["Story3"] = "Another story here"
}
for i = 1, #text["Story1"] do
script.Parent.StoryTeller.Text = text["Story1"]:sub(1, i);
wait()
end
This will go through each story, and play them by looping through the text array with a 1 second delay between lines.
local text = {
"I have been trapped in this wasteland for over 5 years...",
"Text here blah blah",
"Another story here"
}
for StoryLine = 1, #text do
local Story = text[StoryLine]
for i = 1, #Story do
script.Parent.StoryTeller.Text = Story:sub(1, i);
wait()
end
wait(1)
end
You can actually loop through the elements of a table without hard-coding its length.
for storyLine, story in pairs(text) do -- this is a generic for loop
for i = 1, #story do
script.Parent.StoryTeller.Text = story:sub(1, i)
wait()
end
wait(1)
end
It’s better to stay away from hard-coding anything when you can, since it’s much easier to just add another key-value pair to the table than it is to look for everywhere he might’ve hard-coded its length.
That was my original code, but I changed it to a hardcoded length because dictionaries have no order. Meaning the story played in a random order, rather than the desired one.
Ideally, you’d set text to an array, rather than a dictionary.
I updated my code with an array instead, because you are correct hardcoded values aren’t the best.