Is there a way to insert table into another table with table.insert()?

Here are some more details of what i want to achieve

local mytable = {
	games = {"roblox","minecraft"},
	random = {"radio","umbrella"}
}

print(mytable)
--[[
Output:
games = {"roblox","minecraft"}
random = {"radio","umbrella"}
]]

print(mytable.games)
-- prints: "roblox","minecraft"
-----------------------------------------------

local player = "Bob"
local newtable = {}
table.insert(newtable, {player})
table.insert(newtable[player], "Good boi","happy")
print(newtable)

--[[
prints error
i want it to print:
Bob = {"Good boi","happy"}

also should work with:
]]
print(newtable.Bob)
-- should print: "Good boi","happy"

---------------------------------------------------------

local twotables = {}
table.insert(twotables, table.create(1,"Value"))
print(twotables)
-- this does work but it prints like this: {{"Value"}} instead of what i want: {LVL = {"Value"}}
2 Likes

If I understand this correctly, you want to insert a new table to a specific key in another table?

When creating tables I usually do something along the following lines:

local t1 = {
    ... -- Values
}
-- Or:
local t1 = {}
local t2 = { "Value"}
t1["Lvl"] = t2 -- Insert t2 at key "Lvl".
-- This gives t1: { Lvl = { "Value" }}
2 Likes

Hello there,

There’re a several articals about it:

Best of luck!

2 Likes

i think i am making it a bit complicated but my goal is to somehow do something like this:

local bases = {
             AlexMiskevkiller = "Red"
             player2 = "Green"
      }

also the players add themselves with PlayerAdded event
then my objective is to know when player leaves which base was his so i can destroy it

Then this might help:

local bases = {}

game.Players.PlayerAdded:Connect(function(plr)
	bases[plr.Name] = <their team> -- Adds their name into the list along with
                                      -- their team name.
end)

game.Players.PlayerRemoving:Connect(function(plr)
	bases[plr.Name] -- This will give you which base they have.
end)

I would recommend to use their UserId's instead, as they are truly unique.

5 Likes

However, you might be able to achieve this by using Teams instead. Team (roblox.com)

local table1 = {1, 2, 3}
local table2 = {4, 5, 6}
table.foreachi(table2, function(_, value)
	table.insert(table1, value)
end)
print(table.concat(table1, " ")) --1 2 3 4 5 6

You can do this to insert an array into another array.

2 Likes

that helped me thank you very much