Jump to content

Don't understand working function (player, command, ...)


Recommended Posts

Hello. :)

I know, functions in Lua can take variable number of arguments by (...)

So I try this:

  
function testSumm (thePlayer, command, ...) 
    local s = 0 
    for i, n in pairs(arg) do 
         s = s + n 
    end 
    outputChatBox (s, thePlayer) 
end 
addCommandHandler("summ", testSumm) 
  

But this functions works not well. For example, command "/summ 1 2 3" output into the chat 9, not 6.

Where is mistake? Thank you :)

Link to comment

Try this.

function testSumm (thePlayer, command, ...) 
    local s = 0 
    local t = {...} 
    for i, n in ipairs(t) do 
         s = s + n 
    end 
    outputChatBox (s, thePlayer) 
end 
addCommandHandler("summ", testSumm) 

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