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

Error in Text script?

Asked by 9 years ago

I wrote a script with 7 different strings, and I made a loop to display them one at a time, but I can't get it to work. Did I write something in the wrong way?

Script

Label = script.Parent.TextLabel
Tips = {
    "Tips n' Tricks",
    "Claim a Space Agency by touching the grey door in an empty tycoon",
    "Prioritize your cash income before buying optional upgrades",
    "Avoid combat with more powerful players, as you might end losing",
    "Button Colors",
    "Green : Increase your cash income",
    "Red : Optional upgrades"
}

while true do
    for i = 1, 7 do
        Label.Text = Tips[i]
        i = i + 1
        wait(1)
        if i == 8 then
            i = 1
        end
    end
end

The "Tips" table contains every string, while the loop below it displays the text in a TextLabel. The script is below a ScreenGui, which is below StarterGui.

Thanks!

1 answer

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

for loops automatically move i -- you really don't want to change i inside of that loop:

while true do
    for i = 1, 7 do
        Label.Text = Tips[i]
        wait(1)
    end
end

Alternatively, you could just use the while loop and shuffle i around like you were:

i = 1
while true do
    Label.Text = Tips[i]
    i = i + 1
    if i > #Tips then
        -- Instead of `8`, `#Tips`
        -- That way you can add more tips without problems
        i = 1
    end
end

Perhaps a more idiomatic solution that either would be to use an iterator for loop:

while true do
    for _, tip in ipairs(Tips) do
        Label.Text = tip
        wait(1)
    end
end
0
Thank you very much! I don't know very much about pairs and ipairs, so when I tried to write something while I was thinking in that, I merged the 2 loops (while and for) TheArmoredReaper 173 — 9y
Ad

Answer this question