Loop doesn't end

I want the loop to end at a specific time. It is adding on to “elaspedTime” until it reaches timeOut and it should break.

The problem is that the output keeps printing past my timeOut and the loop never ends, which causes the rest of my script to malfunction and not work.

Code

local messages = {"Adding a brand new loading screen. May be updated throughout time.", "Fixed small bugs.", "Fully functional tools shop. All tools are 1 time use only until you buy them again."}

local elaspedTime = 0
local timeOut = 25


while true do
	for _, v in pairs(messages) do
		updates.Text = v
		elaspedTime = elaspedTime + 5
		print("Elasped time = "..elaspedTime)
		wait(5)
	end
	
	if elaspedTime == timeOut then
		print("Text loop complete, ending...")
		break
	end
end

print("Text loop ended;")

Is there a fix?

Try changing it so that it is not

elaspedTime == timeOut

and turn it into

elaspedTime >= timeOut
1 Like

I already tried this, it still has the same output, which doesn’t end.

I added a line after loop to print elapsedTime and timeOut and got this in output:
image

The value doesn’t match because the loop adds the value the number of times there are sentences in the table. You can just put a check code inside a for loop and set an additional value to break a while loop.

These lines of code won’t run until the messages are looped through anyway.

if elaspedTime == timeOut then
	print("Text loop complete, ending...")
	break
end

What is the point in having this?

The while loop is incorrectly utilized.

while elaspedTime < timeOut do
	for _, v in pairs(messages) do
		updates.Text = v
		elaspedTime = elaspedTime + 5
		print("Elasped time = "..elaspedTime)
		wait(5) -- oh yeah, why is this in the for loop?
	end
end

This is how you can utilize the loop at its best.

2 Likes
local messages = {
	"Adding a brand new loading screen. May be updated throughout time.",
	"Fixed small bugs.",
	"Fully functional tools shop. All tools are 1 time use only until you buy them again."
}

local elaspedTime = 0
local timeOut = 25


while wait(5) do
	for _, v in pairs(messages) do
		Updates.Text = v
		elaspedTime = elaspedTime + 5
	end

	if elaspedTime >= timeOut then
		print("Text loop complete, ending...")
		break
	end
end

Tested, and it works!

It isn’t looping through the messages :confused:

It is added so it doesn’t loop through the messages too fast.

if you do not add “break”, the loop won’t stop.

local messages = {
	"Adding a brand new loading screen. May be updated throughout time.",
	"Fixed small bugs.",
	"Fully functional tools shop. All tools are 1 time use only until you buy them again."
}

local elaspedTime = 0
local timeOut = 25

while timeOut do
	for _, v in pairs(messages) do
		Updates.Text = v
		elaspedTime = elaspedTime + 5

		if elaspedTime >= timeOut then
			print("Text loop complete, ending...")
			timeOut = 0 -- break this for loop AND the while loop
			break
		end
                
		wait(5);
	end
end

The break statement should occur in the for loop, which will then shut off the while loop. I hope this helps

It worked for me. Not sure what went wrong.