Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Error in Text script?

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

✓ Accepted answer

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

1 comment

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.