Jump to content

"for loop" small problem! [solved]


pro-mos

Recommended Posts

Posted (edited)
  
function countDown(player, _, limit) 
    for i = 1, limit, 1 do 
        if i ~= limit then 
            msg("[Count Down] " ..i, root) 
        else 
            msg("[Count Down] GO GO GO", root, 255,255,0) 
        end 
    end 
end 
addCommandHandler("c", countDown) 
  

i cant get the else statement to work, its supposed to print "GO" instead of the last limit number

UPDATE: no errors

Edited by Guest
Posted

This should work:

function countDown(player, _, limit) 
    for i = 1, limit do 
        if i ~= limit then 
            msg("[Count Down] " ..i, root) 
        else 
            msg("[Count Down] GO GO GO", root, 255,255,0) 
        end 
    end 
end 
addCommandHandler("c", countDown) 

Posted
This should work:
function countDown(player, _, limit) 
    for i = 1, limit do 
        if i ~= limit then 
            msg("[Count Down] " ..i, root) 
        else 
            msg("[Count Down] GO GO GO", root, 255,255,0) 
        end 
    end 
end 
addCommandHandler("c", countDown) 

no it doesnt

Posted
I just tested it and it worked fine. Don't forget to type the limit when you use the command !!!

its not that different from my code.. it only prints the first if statment, and never prints whats inside else "GO GO GO"

Posted (edited)

Oh, my bad. I didn't get you the first time.

There you go:

function countDown(player, _, limit) 
    local limit = tonumber(limit)+1 
    for i = 1, limit do 
        if i < limit then 
            msg("[Count Down] " ..i, root) 
        else 
            msg("[Count Down] GO GO GO", root, 255,255,0) 
        end 
    end 
end 
addCommandHandler("c", countDown) 

BTW it would be better if you make it with timer (per 1 second).

Good luck!

Edited by Guest

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...