I am making a disaster script and I want the end of the round to display the names of the survivors which are derived from the table. I want to change the table into a string and change the gui text into the string. Can somebody simply show me how to properly use table.concat to turn the table into a string?
The concat
in table.concat is short for concatenate (if you didn't already know). Just thought I'd mention that so you know why the name of the function makes sense.
The function has two mandatory arguments, and two optional arguments. Obviously, the first two mandatory arguments would be the table (of which elements are concatenated), and a string (the character(s) in between each concatenation). Here's an example:
local t = {"a", "b", "c", "d"} print( table.concat(t, " , ") ) -- > "a , b , c , d"
The code above converts the given table to a string of all it's elements which are separated by the string passed as the second argument. So that leaves us with the last two (optional) arguments...
The last two arguments are integers, which represent the index range of the table to include in the concatenated string. For example, let's say you wanted all the elements in your table listed, except for the first and last. This is where you could use range:
local t = {"a, "b", "c", "d"} print( table.concat(t, " , ", 2, #t-1) ) -- > "b , c"
This is telling the function to start at the second index of the table, and list all of the elements up until the last index of the table subtracted by one (the #t-1
expression, which is the last index of the table minus one).
Hope this helped, let me know if you have any questions.