Hello, I want to make a shop with Self-Typing Sentences and it works for the first part. Here is my code: It needs to run the script when I clicked the TextButton.
txtBox = script.Parent.Parent.TextLabel textString = "You chose: Food, What would you like to eat or drink?" speed = .075 -------------------------------------------- length = string.len(textString) nr = 1 str = "" function onButtonClicked() for i = 1, length do toAdd = string.sub(textString, nr, nr) str = str..toAdd txtBox.Text = str nr = nr + 1 wait(speed) end end script.Parent.MouseButton1Down:connect(onButtonClicked)
Your script is not retyping the string because nr will get to be larger than the string's length. This is because you never reset the value of nr to 0. Since everything past the written string is a empty value, the script does not add anything to the TextLabel. Your code is also inefficient as there are variables you don't necessarily need.
You can get rid of the nr variable entirely as well as the length and str variable.
With the revised script, I kept the variables TxtBox and textString. After defining those I set up a function to be connected to later on in the script. With the function I made a for loop that utilized the string's length function directly. I then took the TextLabel and set its Text property to the sub of the string from 1 to i
. What i
is in a numeric for loop is basically a variable. The for loop will make i
count up until it reaches the end, which in this case would be the string's text length. Within the loop I put the wait function with .075 in the parenthesis. Finally the script should end with the connection line to the function.
txtBox = script.Parent.Parent.TextLabel textString = "You chose: Food, what would you like to eat or drink?" function Clicked() for i=1, textString:len() do txtBox.Text = textString:sub(1,i) wait(.075) end end script.Parent.MouseButton1Click:connect(Clicked)