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

How do you make a for i loop only start when a condition has been met?

Asked by
Xonois -3
5 years ago

I'm trying to figure how to only make the for i loop for my game start when it's met a condition. Here's my script:

if script.Parent.Parent.Visible == true then
        for i = 1, #text do
        script.Parent.Text = string.sub(text, 1, i)
        wait(0.01)
    end
end
0
That works though? Put it in an event so it runs each time it fires. User#19524 175 — 5y
0
Nothing is working ¯\_(?)_/¯ Xonois -3 — 5y
0
string.len(text) over #text possibly? fredfishy 833 — 5y
0
try putting it in a while loop? make sure to have a wait in it so it doesn't crash studio ¯\_(+_+)_/¯ mixgingengerina10 223 — 5y

1 answer

Log in to vote
0
Answered by
Wutras 294 Moderation Voter
5 years ago

The attempt you made is pretty close already. What you're missing is an event that'll fire each time script.Parent.Parent changes, so that the script checks it every time. For that we'll do the following:

script.Parent.Parent.Changed:connect(function()
    if script.Parent.Parent.Visible == true then
        -- To make sure that #text is greater than 1, I'd also wrap this inside a condition like this:
        if #text >= 1 then
            for i = 1, #text do
                script.Parent.Text = string.sub(text, 1, i)
                wait(0.01)
            end
        end
    end
end)

Hope that helps. If it doesn't, feel free to ask further questions in the comments to this.

Ad

Answer this question