What is the purpose of . . . in a function?

So on the DevHub I noticed them using ... as a function param sometimes. Just wondering what it is.

For example:

function thing(msg, ...)
print(msg, ...)
end

That’s a viardic parameter. It basically means you can give it a long number of parameters. Keep in mind they are not packed int oa table so you hae to do that yourself. Examples

function func(...)
    print(...) 
end

func(3,8,7) -- Prints 3 8 7
function func2(...)
   local tbl = {...}
   for _,v in pairs(tbl) do
       print(v)
   end
end

func2(3,9,6,5)

--Expected output hopefully
3
9
6
5

https://developer.roblox.com/en-us/articles/Variadic-Functions

Ohhhhhhh! Alright I get that now. Thank you.

Anytime! If you hae anymore issues don’t be afraid to make another post! Also, I recommend you mark it as the solution if it has helped you out so others will know it has been solved!

Also I"m going to edit it to feature an article on the dev wiki that explains it in more detail

How would I print that numerically? I order.

I’m confused a bit, what do you mean? You mean if you give it a random table with random numbers and you want it sorted?

Yeah, exactly that. word limit ignore this

So what you did but they are sorted.

Also, if you use loadstring you can do stuff like this:

local str = [[
local output = table.pack(...)
for _, p in pairs(output) do
    print(p)
end
]]

And then doing this to load it:

local func = loadstring(str)

func("Hello","World!") -- prints: Hello World!
2 Likes

You can use table.sort

function sortNumbers(...)
	local tbl = {...}
	local sorted = table.sort(tbl)
	print(sorted)
end

sortNumbers(3,8,5,2)

It’ll print out a sorted table which will I believe return it in descending order So,

8
5
3
2

Since by default it compares 2 of the things i nthe table via the < operator

Wait it is that simple? Wow, ok. Thanks.

1 Like

It just prints nil for mme… ch limit ignore

Wait hang on I’m dumb. 2 corrections.

  1. It prints in Ascending order,
  2. table.sort returns nothing so the code sohuld be this
function sortNumbers(...)
	local tbl = {...}
	table.sort(tbl)
    print(tbl)
end

sortNumbers(3,8,5,2)

Will print a table with these in order

2
3
5
8

Prints table: 0xf82860.

limit

It works fine for me when I test it in studio, where are you testing it?

Google lua console.

limit limit

I don’t believe they output the contens of the table if you tell it to print the table as it’ll print the table’s address, if you want it to work for lua consoles, you have to do this

function sortNumbers(...)
	local tbl = {...}
	table.sort(tbl)
	for _,v in pairs(tbl) do
		print(v)
	end
end

sortNumbers(3,8,5,2)
1 Like