Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why we can use ... as the arguments when calling a function?

I know that declare a function, ... would act as tuple, for example:

local function hey(...)
	local t = {...}
	local Concat = ""
	for i, v in pairs (t) do
		Concat = Concat..v
	end

	print(Concat)
end

hey(1, 2, 3, "hey") --output: 123hey

But the thing I don't know that is we can use ... as the arguments when call a function too, you can see the example in here(go to line 30-32).

Can someone explain for me, thanks :D

2 answers

✓ Accepted answer

It just means it can hold undetermined amount of parameters

example :

function PrintStuff ( . . . ) -- A function with tuple parameter
	local tupleTable = { . . . } --Putting it in a table variable so we can access it easily
	print (tupleTable[1]) -- Print the first parameter
	for _,v in pairs(tupleTable) do
		print (v) -- Print all the object in the table
	end
end

PrintStuff ("Hello", 3, 2, 'A') -- Printing 4 different stuff
PrintStuff ("Ok", "screeeee") -- printing 2 stuff
PrintStuff (3.1415926) -- printing a single thing

5 comments

0

I mean when we call the function not when we declare it.

1

You simply give the tuple from where the original tuple is from to the function when you call it, i checked the link you gave, it takes whatever parameters inside line 30's tuple, and then give it to line 31, which fired Line 53's function and give whatever parameter inside the tuple to the function, which is the part that triggers line 30's .Touched event.

0

I get it now, thank you for answered my question.

0

Quick thing to add, in lua, tables start with the index at 1 not 0. But don't worry about it too much, it's a common mistake especially if your main language is not lua.

0

@SteamG0D yes you are rightt, thanks

I know this is already answered, but I'd like to add on to what Azure said.

So in the Java programming language, there's a concept called "overloading", it means that you can have 2 functions with the same name but 2 different sets of arguments:

function a(arg1)
    print(arg1)
end

function b(arg1,arg2)
    return arg1+arg2
end

Unfortunately, this will not work in lua, which is where the ... argument comes in. You can do the same thing as above, just structured differently:

function a(...)
    local args = {...}
    if #args == 1 then
        print(args[1])
    elseif #args == 2 then
        return args[1]+args[2]
    end
end

I just checked for the number of arguments in the table because I knew the first function took 1 argument and the second took 2, but you can also check the type and value of the arguments to set them apart allowing you to have more freedom in adding as much functionality as you want to the function a().

2 comments

0

Thank you for explain more deeper to me, it really helped me.

0

This method is also very useful in module scripts if you know about them.

This discussion is preserved as it was. New answers, comments, and votes are closed.