Is there a table.join function in luaU?

Hello, I’m taking AP computer science principles and they are teaching us tables in javascript.

They had this pretty cool function, table.join()

How it works is you put the name of the table following by “.join” and with the parameters of the things you want to seperate the items with. It will return a string of the full table seperated with the thing.

For example

local colors = {"red", "pink", "blue"}
print(colors.join(" and ")

Which would print

"red and pink and blue"

There is not a table.join function on Luau, the closest replacement for your situation is string.format.

Please note that this question can easily be answered by consulting the official Roblox documentation site (found in above hyperlink). Containing the answers to any questions about Luau that may be required.

Yes there is, it’s called table.concat()

2 Likes
local colors = {"red", "pink", "blue"}
print(table.concat(colors, " and "))
1 Like

As long they’re both arrays you can join them as such:

local joinedTab = {
    table.unpack(tabA),
    table.unpack(tabB),
}

What this does is it unpacks each array into a tuple type, which can be used to instantiate values in a table constructor {}.

1 Like

They aren’t looking to combine two arrays into one, they are looking to concatenate the values of the array into a string divided by " and " as shown here

This exact behavior can be replicated using table.concat.

By the way, that doesn’t work even if the question was about joining tables:

tabA = {"Item", 2, 3}
tabB = {"Something", "Thing", 42}
local joinedTab = {
    table.unpack(tabA),
    table.unpack(tabB),
}
for i, v in ipairs(joinedTab) do
  print(i, v)
end
1	Item
2	Something
3	Thing
4	42
1 Like

This can be difficult in this case scenario. I tried checking both Lua documentations and Roblox documentations but I was unable to find a solution. I would never ask a question here unless I wasnt able to figure it out with my available resources. It takes more time to write a post than google it.

In that case, try going less specific, and instead of searching for something pertaining to JS specifically or something similar you could try searching for just the table functions. Doesn’t always work but could be helpful in scenarios like this.

I dont see an issue of me making a dev forum post in the correct category with a valid issue

That wasn’t what I was trying to say, I was just trying to help you in the future because you said:

when more refined searches can index something like this:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.