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

Why is my script stopping at a certain time?[SOLVED]

Asked by 9 years ago

My script goes a certain length, then stops. Here it is:

while wait() do
    if game.Workspace.Text1Ready.Value == true then
        script.Parent.Text1.Visible = true
        for i = 1, string.len("Commander: Hello Soldier, are you ready?") do
            wait(0.1)
        script.Parent.Text1.Text = string.sub("Commander: Hello Soldier, are you ready?", 1, i)
        end
        game.Workspace.Talking.Value = false
        script.Parent.Text2.Visible = true
            script.Parent.Text2.Text = "CLICK ANYWHERE TO CONTINUE"     
            script.Parent.DarkScreen.Visible = true
            game.Workspace.Typewrite:Stop()
            game.Workspace.Text1Ready.Value = false
        script.Parent.DarkScreen.MouseButton1Down:connect(function()
        game.Workspace.Text2Ready.Value = true
        script.Parent.Text2.Visible = false
    end)
    end
    if game.Workspace.Text2Ready.Value == true then
    game.Workspace.Typewrite:Play()
            script.Parent.DarkScreen.Visible = false
        for i= 1, string.len("Commander: Because if not, we can take you back to where you came from.") do
            wait(0.1)
        script.Parent.Text1.Text = string.sub("Commander: Because if not, we can take you back to where you came from.", 1, i)
        end
        wait(4) 
        for i= 1, string.len("Commander: If you want to go weewee in your big boy slacks, now's the time.") do
            wait(0.1)
        script.Parent.Text1.Text = string.sub("Commander: If you want to go weewee in your big boy slacks, now's the time.", 1, i) -- Stops at 'C' in commander.
    wait(4)
    game.Workspace.Text1Ready.Value = false
    game.Workspace.Loading1.Value = true
    game.Workspace.Talking.Value = false
    for i,v in pairs(game.Workspace:GetChildren()) do
        if v:IsA("Sound") then
            v:Stop()
        end
    end
         end
        script.Parent.Text1.Visible = false
        script.Parent.Text2.Visible = false 
    end
    end

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

First: tab your code correctly

Now, various other style improvements

  • Use a variable for the long pieces of text instead of repeating them twice in the code
  • Use the string methods instead of string.

Now that the code is properly tabbed, we can see there's an obvious problem:

        local text = "Commander: If you want to go weewee in your big boy slacks, now's the time."
        for i = 1, #text do
            wait(0.1)
            script.Parent.Text1.Text = text:sub(1, 4) -- Stops at 'C' in commander.
            wait(4)
            game.Workspace.Text1Ready.Value = false
            game.Workspace.Loading1.Value = true
            game.Workspace.Talking.Value = false
            for i,v in pairs(game.Workspace:GetChildren()) do
                if v:IsA("Sound") then
                    v:Stop()
                end
            end
        end

You're doing a bunch of stuff inside the for loop for the text. You should be doing it after the for loop (this is why we tab our code correctly).


You show a message character by character three times (maybe you'll do it more?) so you should use a function to do that:

e.g.,

    if game.Workspace.Text2Ready.Value == true then
        game.Workspace.Typewrite:Play()
        script.Parent.DarkScreen.Visible = false
        say("Commander: Because if not, we can take you back to where you came from.")
        wait(4) 
        say("Commander: If you want to go weewee in your big boy slacks, now's the time.")
        wait(4)
        game.Workspace.Text1Ready.Value = false
        game.Workspace.Loading1.Value = true
        game.Workspace.Talking.Value = false
        for _ ,v in pairs(game.Workspace:GetChildren()) do
            if v:IsA("Sound") then
                v:Stop()
            end
        end
        script.Parent.Text1.Visible = false
        script.Parent.Text2.Visible = false 
    end

Now it's not even possible to make the mistake you made before.


I would suggest a different organization for your code. Everything in a big while loop doesn't really capture what you want to do. Neither do a new BoolValue for every transition.

Consider doing something like this instead:

function say(text)
    for i = 1, #text do
        wait(0.1)
        script.Parent.Text1.Text = text:sub(1, i)
    end
end

function scene1()
    script.Parent.Text1.Visible = true
    say("Command: Hello Soldier, are you ready?")
    game.Workspace.Talking.Value = false
    script.Parent.Text2.Visible = true
    script.Parent.Text2.Text = "CLICK ANYWHERE TO CONTINUE"
    script.Parent.DarkScreen.Visible = true
    game.Workspace.Typewrite:Stop()
    script.Parent.DarkScreen.MouseButton1Down:wait()
    script.Parent.Text2.Visible = false
end

function scene2()
    game.Workspace.Typewrite:Play()
    script.Parent.DarkScreen.Visible = false
    say("Commander: Because if not, we can take you back to where you came from.")
    wait(4) 
    say("Commander: If you want to go weewee in your big boy slacks, now's the time.")
    wait(4)
    game.Workspace.Loading1.Value = true
    game.Workspace.Talking.Value = false
    for _ ,v in pairs(game.Workspace:GetChildren()) do
        if v:IsA("Sound") then
            v:Stop()
        end
    end
    script.Parent.Text1.Visible = false
    script.Parent.Text2.Visible = false 
end

scene1()
scene2()
0
Thank you so much for putting time to answer this. It solved a huge chunk, and it made me learn the best thing ever! deputychicken 226 — 9y
Ad

Answer this question