Unlimited args into one variable


#1

Hello, I was hoping someone could help me out with putting all arguments someone has typed in after a RegisterCommand() has been called, into one single string variable to put into the chat, and also into a table. So far, I tried unpacking args but that didn’t work, it just failed to concatenate the variables. I also tried literally assigning args[1] through to args[10] to a variable and concatenating them, but that didn’t work either so I think i’ve reached the level of needing assistance with this really. Just point me in the right direction and i’m sure I can come up with something :slight_smile:

Thanks for reading <3


#2

There you go :slight_smile: https://www.lua.org/pil/5.2.html

Read 5.2 – Variable Number of Arguments


#3

You can also do

  local theFunc = function(foo, bar, ...)
    
    local extraArgs = {...}

    for i=1, #extraArgs, 1 do
      -- DO SOMETHING HERE
    end

  end

#4

Oh, perfect thank you for the quick answer!!

Could you possibly shove all that here as a working example? This is what I have so far… I need to get all the arguments and shove 'em into one variable. I won’t include the entire RegisterCommand because there’s just no point, but the important part is the act that we have args as a variable passed by it, and I want to have this working…

        local marker = table.unpack(args)
        print(marker)
        TriggerEvent('chatMessage', "^3" .. marker .. "^4^* Added to vehicle: " .. "^3" .. plate)

        table.insert(pings, {plate, primary, vehicle, marker})

But obviously, I need to replace table.unpack with what you’re suggesting, I just don’t understand how to incorporate it exactly, I haven’t had my morning tea yet haha. Thank you again for the quick response though! :slight_smile: 3


#5

imagine your args are plate, primary, vehicle, marker

RegisterCommand("thecommand", function(source, args, raw)
   local plate, primary, vehicle, marker = table.unpack(args)
  -- Should do the job in this case
end, false)

#6

I know that would work, but that’s not what the args are supposed to be, sorry I should’ve specified. I have those details automatically being put into variables in the rest of the command, I just didn’t think it was necessary to send the entire thing. This is for an ANPR script, so in this case what I want is when you do the command, it gets the plate, primary colour, vehicle model and sends it in chat whilst also putting it into a table. So far, everything works apart from the fact that I also need the user to be able to type in chat what they want the marker to be there for, for example…

/anpr Vehicle is Stolen

and what then needs to happen, is those 3 words there need to be put into one single string to put into both the chat event, and the table. Does that make more sense? Sorry, I should’ve specified… My fault.


#7

This will be my last reply on this topic but you can do

local str = ''

for i=1, #args, 1 do
  if i > 1 then str = str .. ' '
  str = str .. args[i]
end

-- Here str == 'Vehicle is Stolen'

#8

Thank you! That works perfectly