Copy one table into another table?

How do I append one table into another table?

The first table is part of a function that passes the player as an argument into the table as such:

	local tab = {
		name = player.Name,
		userId = player.UserId
	}

and the second is in a module script like so:

local playerInventory = {}

playerInventory.Currency = {}
playerInventory.Ingredients = {}
playerInventory.Potions = {}
playerInventory.Tools = {}
playerInventory.Backpack = {}
playerInventory.PotionPack = {}

playerInventory.Currency = 100

playerInventory.Ingredients.Grass = 10
playerInventory.Ingredients.Sand = 10
playerInventory.Ingredients.Slime = 10

playerInventory.Potions.Potion1 = 10
playerInventory.Potions.Potion2 = 10
playerInventory.Potions.Potion3 = 10
playerInventory.Potions.Potion4 = 10
playerInventory.Potions.Potion5 = 10
playerInventory.Potions.Potion6 = 10
playerInventory.Potions.Potion7 = 10
playerInventory.Potions.Potion8 = 10
playerInventory.Potions.Potion9 = 10
playerInventory.Potions.Potion10 = 10
playerInventory.Potions.Potion11 = 10
playerInventory.Potions.Potion12 = 10
playerInventory.Potions.Potion13 = 10
playerInventory.Potions.Potion14 = 10
playerInventory.Potions.Potion15 = 10

playerInventory.Tools.Wand1 = 1
playerInventory.Tools.Wand2 = 1
playerInventory.Tools.Wand3 = 1

playerInventory.Backpack = "Best"

playerInventory.PotionPack = "Best"
	
    return playerInventory

I have required the module into the main script, just not sure how to append them into an existing table with data already in it.

There is probably a better way to do this, thanks in advance for the support.

1 Like

What you could do is just loop through the original table, and set the index and values from that, to the new table.

Example:

local a = {}
a.Hello = "Hello"
a.Bye = "Bye"
local b = {}

for i,v in pairs(a) do
b[i] = v
end

for i,v in pairs(b) do
print(i,v)
end

This was written on mobile, and not tested

1 Like

Would that work if b already had values in it we do not want to loose?

Yes, it would still work.
For future reference, please reply to my reply, instead of creating a new reply

Got it, thanks.

One caveat though is that, in this example, keys in a will override keys in b. For instance, if a.test is 32 and b.test is 64, then the code will override the 64 and change it to 32.

If you want to prevent that, you could just throw in a simple condition to see if the key doesn’t exist in b:

for i,v in pairs(a) do
   if (b[i] == nil) then
      b[i] = v
   end
end

Note that explicitly checking for nil is required, since the key could have a false value instead possibly.

2 Likes

Wow thanks so much for all your help folks! I feel like I am learning fast and I couldn’t do that without you all.

I changed my mark of solution to @Crazyman32 answer because, while the original was also correct, his response added some depth about not copying over data inside existing keys.

I know this was a really basic question but perhaps it will server to help someone else sometime.

Id hate to necro, but @Crazyman32’s solution doesn’t seem to work for me and I don’t want to start a dupe topic.

The problem I’m having is whenever I try to “Copy” one table into another, the table remains linked to the other table. Test it for yourself:

The print should print 0 every time if it was truly a new table, but it doesn’t, and NewData just acts as a proxy to set DefaultData

local DefaultData = {
		Stored = {
			Kills = "0",
			Deaths = "0",
			OwnedShip = "01|",
			OwnedSkin = "01|",
			OwnedExhaust = "01|",
			OwnedRing = "01|",
			OwnedFlares = "01|",
		},
		Temp = {
			Ship = nil,
			AB = 0,
			Kills = 0, 
			Deaths = 0
		}		
	}
local RepeatTill = 0
repeat
	local NewData = {}
	for i,v in pairs(DefaultData) do
		NewData[i] = v
	end
	print(NewData.Temp.AB) -- Observe that this should print 0 if it was a new table
	NewData.Temp.AB = 123
	RepeatTill = RepeatTill + 1
until RepeatTill == 6