Jump to content

"for loop" small problem! [solved]


pro-mos

Recommended Posts

  
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
Link to comment

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) 

Link to comment
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

Link to comment

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