Jump to content

[Help] While loop not working


ManeXi

Recommended Posts

I have created this while loop that it should add one number each 1 second until 10 for some reason the script crashes saying:

ERROR: Aborting; infinite running script in whileLoopTest

local n = 0 
while (n <= 10) do 
    if not (isTimer(timer)) then 
        timer = setTimer(function() 
            outputChatBox("Number: "..n) 
            n = n + 1 
        end, 1000, 1) 
    end 
end 

for some reason without timer works well

local n = 0 
while (n < 10) do 
    n = n + 1 
    outputChatBox("Number: "..n, root, 255, 0, 0) 
end 

So... my question is: Is there any way to delay the while loop actions?

Link to comment

Maybe it's because this statment which doesn't not follow the creation of the timer due to it's existance. I would take a look at the isTimer and try to convert it into a variable.

  
if not (isTimer(timer)) then 
  

Try this:

  
    local n = 0 
    local check = isTimer( timer ) 
    while (n <= 10) do 
        if not ( check ) then 
            timer = setTimer(function() 
                outputChatBox("Number: "..n) 
                n = n + 1 
            end, 1000, 1) 
        end 
    end 
  

Or you could make this with a numerical loop.

  
local number = 0 
for i = 1, 20 do 
 number = number + 0.5 -- For making it last more. 
end 
if number == math.floor( number ) then 
 outputChatBox("Number: ".. n) 
end 
  

Link to comment

If you want to crash your game, go ahead:

  
local n = 0 
local h1, h2, h3 = debug.gethook() 
debug.sethook(nil) 
while (n <= 10) do 
    if not (isTimer(timer)) then 
        timer = setTimer(function() 
            outputChatBox("Number: "..n) 
            n = n + 1 
        end, 1000, 1) 
    end 
end 
debug.sethook(_, h1, h2, h3) 
  

This should stop the error but will lag your client lol.

EDIT: I suppose you want to run timer 10 times? Use it like:

  
local n = 0 
timer = setTimer(function() 
    outputChatBox("Number: "..n) 
    n = n + 1 
end, 1000, 10) 
  

Edited by Guest
  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...