Add key and Value into a table when the key is named with a variable

Hello, i have need some help to put key named with a variable inside a table, here the issue:

local module = {}

local Inventory = {} --The table that store all the ressource

function module.Handle(plr)
	local playerInventory = plr.Inventory
	
	for i,v in pairs(playerInventory:GetChildren()) do
		print(v.Name) --print "Stone", and "Wood" for the second turn
		Inventory.v = 50 --over here it put the "v" key with 50 as value inside Inventory, but it should put v.Name (so Stone)
	end
	print(Inventory) --print { ["v"] = 50 } instead of this: { ["Stone"] = 50,   ["Wood"] = 50 }
end

return module

change this line

Inventory.v = 50

to

Inventory[v.Name] = 50

1 Like

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