How do i merge two tables together?

How do i merge two tables together? Example:

local table1 = {
	"hello1",
	["hello2"] = {
		yes = true
	}
}
local table2 = {
	"hello3",
	"hello4"
}
local mergedTable = {
	"hello1",
	["hello2"] = {
		yes = true
	},
	"hello3",
	"hello4"
}

3 Likes

Using “FOR” loop and table.insert, is the most easily way.

local table1 = {
	"hello1",
	["hello2"] = {
		yes = true
	}
}
local table2 = {
	"hello3",
	"hello4"
}
local FinalTable = {}

for _, TableChild in pairs(table1) do
    table.insert(FinalTable,table1)
end

for _, TableChild in pairs(table2) do
    table.insert(FinalTable,table2)
end

12 Likes
local table1 = {
	"hello1",
	"hello2"
}

local table2 = {
	"hello3",
	"hello4"
}

table.move(table2, 1, #table2, #table1 + 1, table1)
for _, value in ipairs(table1) do
	print(value)
end

table.move() is also available for arrays.

11 Likes

Hm i didn’t knew that, this is a new function? I am not who is asking but it’s will help me too. :stuck_out_tongue:

It’s a backported feature from Lua 5.3, I’m not sure exactly when it was added to Roblox Lua.

You can find its documentation here.
https://www.lua.org/manual/5.3/manual.html
https://developer.roblox.com/en-us/api-reference/lua-docs/table

3 Likes

Oh, ty. I studed with lua 4.9 so i didn’t knew that.

1 Like