Needed help with understanding the "..." in functions

Hello
In the example below, ... returns only the first argument correctly and the rest is nil. Why is that happening?

local function dd(a1,a2)
    print(a1,a2); -- true,nil
end

function(bool,...)
    local Arguments = ...; -- I could make a table out of this, but I don't want to achieve such goal, i want to have multiple arguments in the `dd` function
    
    if bool then
        dd(ArgumentsForAnotherFunction);
    end
end

If someone could tell me more about the ... (and most importantly how is it called), then it would be great and I would really appreciate it.

If something is unclear then please let me know.

Thanks, have a nice day!

2 Likes

I believe people refer to them as concatenations, but I don’t quite remember if that is the correct name to the top of my head. People call them different things in different programming languages. They are basically used to combine two strings together. So lets say we have a variable called “response” which has a sentence in it and we want to print that out with another word. Here is what we would do.

local response = "This is a response"

print(response..." that is cool") -- the concatenation helps combine those two strings together.

expected output: This is a response that is cool

I thought it used 2 … instead of 3. I wasn’t going to answer it because I thought it was a different thing, but it is indeed a concatenation.

1 Like

This is also my first time knowing this thing. I thought the 3 dots are for some example that doesn’t do anything that’s why I don’t pursue learning it.

Yep the correct way for Concat is 2 dots only.

1 Like

I believe you are talking about variadic function s:

Here’s some learning resources about them:

https://www.lua.org/pil/5.2.html

3 Likes

Thanks for this! Now my question is, Is this a good way to use when developing something? I mean is this really helpful? Sorry if I’m asking so stupid, it’s just I don’t know how,when to use this.

Yeah they can be pretty useful, one way I use them is to test out remote events.

Just do function(…) Print(…) End and it’ll print all the parameters for quick easy testing. I believe there are more that I have used but I forgot. I’ll edit the post later maybe.

1 Like

But what about my original question?

The way I use it, turn the … into a table and call individual stuff from the args
ex.

local Args = {...};
if bool then
    SendFunc(Args[1], Args[2])
end

Thanks, however the number of arguments may vary, and I have a type of system where I don’t know how many arguments are being parsed. How can I solve it?

Just check the length of the arguments table?
#args

How would checking the length help me in this situation?

For this issue the only reason the first argument is passed is because well, you only defined one variable here is what it would look like without the (…) notation

local Arguements = arguement1,arguement2,arguement3
--yeah arguement2 and arguement 3 just go poof

One way to get around this is to pack them like what @ObserverBot has done. Or you can use table.pack()

local Args = table.pack(...);

Yep what @twinqle said would work for non-nil arguments but, keep in mind if an argument is nil then it wouldn’t be counted.

Here’s a special thing that table.pack does which gets the number of arguments in the (…) notation, if you wanted to.

    local test = table.pack(1,2,3,4,nil,6)
    print(test.n)  --6 arguments

Edit: Yeah for the code sample given, to easily pass all the arguements to the function dd just pass … to it, no need to table.pack() if you wish to forward the arguments in the same order.

Another example of the usage of … is the advanced debounce function which makes any function inputted into it able to have a debounce.

1 Like

If you just want to give all the arguments to dd, you can do

dd(...)
1 Like

So I would need to use a table? Ehh… alright then, thank you.

1 Like

Well, that would’ve worked, but…
obraz

I call the function inside another function, so basically it looks like this:

local function dd()

function(...)
    function()
        dd(...) -- error here

and I need to use a variable for that

If you want to use dd(…) you can do this:

local function dd(...)
local a1,a2 = ...
    print(a1,a2); -- true,nil
end

Keep in mind the rest of the arguments will be thrown away, it might be better to print(…) instead so it supports all the variables inputted which is the purpose of … variadic functions.

1 Like
local function dd(...)
    print(...);
end

local function someFunction(bool, ...) 
    if bool then
        dd(...);
    end
end
3 Likes
local TableCount = #Args

Just add that under the args var :slight_smile: