I was trying to make a variable and then I noticed that "select" is a thing. What does it do and can I also have an example? Thx in advance!
What does "select" do, extra words so I can post this thread?
3 answers
select(n, ...) in lua returns the nth argument and everything else after it.
Here is a few examples
print(select(3, 0, 1, 2, 3, 4))
--> 2 3 4
print(select(1, 'a', 'b', 4))
--> a b 4
If you want to get how many arguments were given excluding the selector pass "#".
print(select("#", 1, 2, 3, 4, 5))
--> 5
2 comments
do not be a hypocrite. you also copied the documentation, but just changed the order of words and included the fact that # is a valid argument
Where do i copy it, and i would never exclude an important feature of select, that if string "#" is passed it gives you how many arguments are given
select is very useful, for example let's say you have a function that returns multiple values, something like this
local function something(a, b, c)
return a, b, c
end
select makes you choose what to return. Its first paramater is a can be a number, and if so, it will return all values after the value with the corresponding number and also including it
So if we try this out
print(select(2, something("hi", "cool", "yo"))
--prints "cool" "yoo"
This can be useful for things like :FindPartOnRay() which returns
a lot of values (the first value is the part that was hit with the ray, the second is the exact position of where the ray hit), what if you just needed the second value. Select would be useful.
select also can have a string that has to be written like this "#",
if so it will return how many returned values there
print(select("#", something(7, "hj", true)))
--prints 3
So, more detail that I'm gonna mention. something returns 3 values next to each other, they're not a table, you can think of them as just of a group of values seperated with a comma ,
(This is actually a datatype, its called a tauple, you can think of it looking like this (value, value, value))
So really, something is returning 3 values like this 7, hj, true 3 values next to each other
--so doing this
select(2, something(7, "hj", true))
--is the same as doing this
select(2, 7, "hj", true)
I hope you got the idea.
Namaste it is i, dual
You can find select() in:
https://developer.roblox.com/en-us/api-reference/lua-docs/Lua-Globals
Quoting the wiki, select "Returns all arguments after argument number index."
It is generally used to access a group of values, returned as Tuple, however there is a special case where it returns the number of elements in the Tuple when passed the # argument.
Perhaps some code examples would work better:
local function a()
return 1, 2, 3, 4, 5
end
print(select("#", a()))
-> 5
local function a()
return 1, 2, 3, 4, 5
end
print(select(1, a()))
-> 1, 2, 3, 4, 5
local function a()
return 1, 2, 3, 4, 5
end
print(select(2, a()))
-> 2, 3, 4, 5
local function a()
return "dlp", "sin", "viking"
end
print(select(3, a()))
-> "viking"
local function a()
return "dlp", "sin", "viking"
end
print(select(4, a()))
-> `` (nil)
On a sidenote, some interesting behaviour: (look at what is returned)
local function a()
return "dlp", "sin", "viking"
end
print(select(4, a()))
-> ``
local function a()
return "dlp", "sin", "viking"
end
print((select(4, a())))
-> nil
That is all
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Select(index,...): If index is a number, returns all arguments after argument number index.
http://www.lua.org/manual/5.1/manual.html#pdf-select