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

How do I properly use table.concat?

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?

1 answer

✓ Accepted answer

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.

Arguments

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...

Range

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.

2 comments

0

Thanks. I'll try using this in my script.

0

Also, is there any way to view the same table from a different script or even script type?

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