Hello fellow developers!
I was wondering if you could store two values in one table slot. For example, I have a knife. The knife has a name and a UUID. I want to store these two values in a table so that I can retrieve it and reset the values.
If it is confusing, please let me know!
Thanks!
Yes, you can. Technically it’s having a table inside table, I’ll get you an example:
local Items = {
Knife = {
name = "name";
UUID = "UUID";
};
}
4 Likes
This is quite helpful, but how would you use the function ‘table.insert’ to do the above.
If you are going to assign string key to the value in the table, instead of table.insert
, you could assign key and value pair by doing table["key"] = value
.
For example:
local Items = {}
Items["Knife"] = {name = "name"; UUID = "UUID";}
-- OR
Items["Knife"] = {}
Items["Knife"]["name"] = "name"
Items["Knife"]["UUID"] = "UUID"
2 Likes
That is one step closer to my problem! Thanks!
Now, how would you retrieve data from that table? Say you wanted to find the UUID. How would you do that?
Again, thanks a LOT for helping me out!
It’s the same way how you refer to instances in game, like game.Workspace.Part
.
local Items = {
Knife = {
name = "name";
UUID = "UUID";
};
}
local name = Items.Knife.name
local UUID = Items.Knife.UUID
-- OR
local name = Items["Knife"]["Name"]
local UUID = Items["Knife"]["UUID"]
1 Like
Here is some context:
My game has a saving system using DataStore of course. What it does is, it loops through the player’s knife inventory and stores each entry into a table:
This is the loading data:
if knivesData then
for _, knife in pairs(knivesData) do
if game.ServerStorage.Items.Knives:FindFirstChild(knife) then
local knifeClone = game.ServerStorage.Items.Knives[knife]:Clone()
knifeClone.Parent = knifeInventory
print(knife .. " [Class: Knife] loaded!")
end
end
-- Set EquippedGunVal
if equippedKnifeData then
equippedKnife.Value = equippedKnifeData
player:WaitForChild("PlayerGui")
game.ReplicatedStorage.Events.SendEquippedKnife:FireClient(player, equippedKnifeData)
end
else
local knifeClone = game.ServerStorage.Items.Knives.DefaultKnife:Clone()
knifeClone.Parent = knifeInventory
local UUID = HTTPService:GenerateGUID()
local stringVal = knifeClone.UUID
stringVal.Value = UUID
knifeClone:SetAttribute("UUID", UUID)
print(knifeClone:GetAttribute("UUID"))
equippedKnife.Value = "DefaultKnife"
print("Player is new! DefaultKnife added!")
end
And the save script:
local knives = game.ServerStorage.PlayerInfo[player.Name].Inventory.Knives:GetChildren()
local knivesTable = {}
for _, v in pairs(knives) do
table.insert(knivesTable, v.Name)
end
weaponsDataStore:SetAsync(player.UserId.."-Knives", knivesTable)
if game.ServerStorage.PlayerInfo[player.Name].EquippedKnife.Value ~= nil then
local equippedKnifeVal = game.ServerStorage.PlayerInfo[player.Name].EquippedKnife
weaponsDataStore:SetAsync(player.UserId.."-EquippedKnifeVal", equippedKnifeVal.Value)
end
print("Saved Knives of user " .. player.Name)
Then I modified the load and save script to match your descriptions:
local knives = game.ServerStorage.PlayerInfo[player.Name].Inventory.Knives:GetChildren()
local knivesTable = {}
for _, v in pairs(knives) do
knivesTable["Knife"] = {}
knivesTable["Knife"]["Name"] = v.Name
knivesTable["Knife"]["UUID"] = v.UUID.Value
end
weaponsDataStore:SetAsync(player.UserId.."-Knives", knivesTable)
if game.ServerStorage.PlayerInfo[player.Name].EquippedKnife.Value ~= nil then
local equippedKnifeVal = game.ServerStorage.PlayerInfo[player.Name].EquippedKnife
weaponsDataStore:SetAsync(player.UserId.."-EquippedKnifeVal", equippedKnifeVal.Value)
end
print("Saved Knives of user " .. player.Name)
Load Script:
for _, knife in pairs(knivesData) do
if game.ServerStorage.Items.Knives:FindFirstChild(knife.Items.Name) then
local knifeClone = game.ServerStorage.Items.Knives[knife.Items.Name]:Clone()
knifeClone.Parent = knifeInventory
print(knife .. " [Class: Knife] loaded!")
end
end
But unfortunately, it doesn’t seem to load. I don’t know if I have provided enough info, but I hope you can help me! Thanks!
From here, you are assigning all values to one key ["Knife"]
, and it’s consistently getting overridden by the values as iteration goes on.
There are two types of solutions you can implement, one is to differentiate all the keys, or secondly, convert the table to an array instead of a dictionary.
First way:
local knives = game.ServerStorage.PlayerInfo[player.Name].Inventory.Knives:GetChildren()
local knivesTable = {}
for i, v in pairs(knives) do
knivesTable[i] = {}
knivesTable[i]["Name"] = v.Name
knivesTable[i]["UUID"] = v.UUID.Value
end
Second way:
local knives = game.ServerStorage.PlayerInfo[player.Name].Inventory.Knives:GetChildren()
local knivesTable = {}
for _, v in pairs(knives) do
local IndexNumber = #knivesTable + 1
knivesTable[IndexNumber] = {}
knivesTable[IndexNumber]["Name"] = v.Name
knivesTable[IndexNumber]["UUID"] = v.UUID.Value
end
3 Likes
I believe that the saving is working. But the loading isn’t 
Here is the loading script:
for key, knife in pairs(knivesData) do
--if game.ServerStorage.Items.Knives:FindFirstChild(knife) then
--local knifeClone = game.ServerStorage.Items.Knives[knife]:Clone()
--knifeClone.Parent = knifeInventory
--print(knife .. " [Class: Knife] loaded!")
--end
local knives = knife[key]
local name = knives.Name
print(name)
local uuid = knives.UUID
print(uuid)
end
Thank you soo much for helping me out so far!
It would be helpful if you post the full code of loading
Heya! I actually figured out what I was doing wrong and it works now!
Before, I was actually calling a nil value:
for key, knife in pairs(knivesData) do
--if game.ServerStorage.Items.Knives:FindFirstChild(knife) then
--local knifeClone = game.ServerStorage.Items.Knives[knife]:Clone()
--knifeClone.Parent = knifeInventory
--print(knife .. " [Class: Knife] loaded!")
--end
local knives = knife[key]
local name = knives.Name
print(name)
local uuid = knives.UUID
print(uuid)
end
But now, I am calling it through the knivesData variable since that is the actual table!
for key, knife in pairs(knivesData) do
--if game.ServerStorage.Items.Knives:FindFirstChild(knife) then
--local knifeClone = game.ServerStorage.Items.Knives[knife]:Clone()
--knifeClone.Parent = knifeInventory
--print(knife .. " [Class: Knife] loaded!")
--end
local knives = knivesData[key]
local name = knives.Name
local uuid = knives.UUID
if game.ServerStorage.Items.Knives:FindFirstChild(name) then
local knifeClone = game.ServerStorage.Items.Knives[name]:Clone()
knifeClone.Parent = knifeInventory
knifeClone.UUID.Value = uuid
end
end
It works now! All thanks to you! Thank you SO MUCH!
I am glad to hear that you have fixed the issue, congratulations