Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you fix this, My script doesnt run?

Asked by
DeepDev 101
9 years ago

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)
0
Alright, so what is the problem? Also, your question title should be a one sentence summary of what the problem is. M39a9am3R 3210 — 9y
0
Well, It litterly does nothing. Nothing in the logs too :/ DeepDev 101 — 9y

1 answer

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

Problem

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.


Solution

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.


Final Script

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)

Hopefully this answered your question, if it did hit the Accept Answer button to the left. If you have any questions, feel free to leave them in the comments below.
0
It is still doing nothing, It keeps it with the previous String. DeepDev 101 — 9y
0
Do you expect the string to change or to retype itself? Because that's what the script should be doing, retyping itself. Is there an error in the output / devconsole? Can you send me a animation of what is going on? M39a9am3R 3210 — 9y
Ad

Answer this question