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

How can I shorten this script?

Asked by 9 years ago

Basically, I am using the typewriter effect that I asked about in my previous question. I am trying to do that for many strings and noticed that it would become quite lengthy. Please do not hesitate to answer because I will be grateful and will try each of them. Thank you in advance for any help you give.

local username = script.Parent.Parent.Parent.Parent.Name
local Time1 = .1
---------------------------------------Strings--------------------------------------------
local str = "Hello, "..username
local str2 = "Sample"
local str3 = "Sample"
local str4 = "Sample"
local str5 = "Sample"
------------------------------------------------------------------------------------------
for i = 5,#str do
    local substr = str:sub(1,i)
    script.Parent.Text = substr
    wait(Time1)
end

wait(1)

for i = 5,#str2 do
    local substr = str2:sub(1,i)
    script.Parent.Text = substr
    wait(Time1)
end

wait(1)

for i = 5,#str3 do
    local substr = str3:sub(1,i)
    script.Parent.Text = substr
    wait(Time1)
end

wait(1)

for i = 5,#str4 do
    local substr = str4:sub(1,i)
    script.Parent.Text = substr
    wait(Time1)
end

wait(1)

for i = 5,#str5 do
    local substr = str5:sub(1,i)
    script.Parent.Text = substr
    wait(Time1)
end

2 answers

Log in to vote
3
Answered by 9 years ago

If there is one thing scripting has taught me about reducing the length of code, it is that you should not copy code multiple times but make a function that can be used to run your code.

So, for this example, we're going to assign arguments that we can use in the code and modify your code to fit the function's argument variables.

function TypewriteText(st,start,time) --St is the string, start is where to start in the string and time is the delay for each type.
    for i = start,#st do
        local substr = st:sub(1,i)
        script.Parent.Text = substr
        wait(time)
    end
end

Now that we have a TypewriteText function, we can just implement this into the code.

local username = script.Parent.Parent.Parent.Parent.Name
---------------------------------------Strings--------------------------------------------
local str = "Hello, "..username
local str2 = "Sample"
local str3 = "Sample"
local str4 = "Sample"
local str5 = "Sample"
------------------------------------------------------------------------------------------

function TypewriteText(st,start,time) --St is the string, start is where to start in the string and time is the delay for each type.
    for i = start,#st do
        local substr = st:sub(1,i)
        script.Parent.Text = substr
        wait(time)
    end
end

TypewriteText(str,5,.1)
wait(1)
TypewriteText(str2,5,.1)
wait(1)
TypewriteText(str3,5,.1)
wait(1)
TypewriteText(str4,5,.1)
wait(1)
TypewriteText(str5,5,.1)

Now that the base of the code is done, we could act on improving this script. Instead of having a bunch of variables, let's make a table with each response in and use ipairs to go through each string in order, do the typewriting effect then wait a second before moving onto the next string.


Final code:

local username = script.Parent.Parent.Parent.Parent.Name
---------------------------------------Strings--------------------------------------------
local strings = {"Hello, "..username, "Sample", "Sample", "Sample", "Sample"}
------------------------------------------------------------------------------------------

function TypewriteText(st,start,time) --St is the string, start is where to start in the string and time is the delay for each type.
    for i = start,#st do
        local substr = st:sub(1,i)
        script.Parent.Text = substr
        wait(time)
    end
end

for _,str in ipairs(strings) do --Go through each string in order of where they are in the table.
    TypewriteText(str,5,.1) --Invoke the TypewriteText function. The script will wait until the function ends before proceeding.
    wait(1) --Wait one second before going to the next string.
end

I hope my answer helped you. If it did, be sure to accept it.

0
Lol, you explained quite a bit more than I did. Upvoted dyler3 1510 — 9y
0
I'll upvote your answer too since you practically had the same idea as me. Spongocardo 1991 — 9y
Ad
Log in to vote
3
Answered by
dyler3 1510 Moderation Voter
9 years ago

A simple way to do this would be to put the strings into a table, than use a single loop to go through each of them. You could do that like this:

local username = script.Parent.Parent.Parent.Parent.Name
local Time1 = .1
---------------------------------------Strings--------------------------------------------
local str = "Hello, "..username
local str2 = "Sample"
local str3 = "Sample"
local str4 = "Sample"
local str5 = "Sample"
Strings={str,str2,str3,str4,str5}
------------------------------------------------------------------------------------------
for _,String in pairs(Strings) do --Loops through all of the strings
    for i = 5,#String do
        local substr = str:sub(1,i)
        script.Parent.Text = substr
        wait(Time1)
    end
    wait(1)
end

Anyways, if this helped at all, an upvote would be appreciated. If you have any further problems/questions, please leave a comment below. Hope I helped :P

-Dyler3

0
Good job, Dyler. Upvoted. Validark 1580 — 9y
0
Thanks :P dyler3 1510 — 9y

Answer this question