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;")
I added a line after loop to print elapsedTime and timeOut and got this in output:
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.
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
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
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