Okay so when I save a multidimensional table to PData it saves with no error, But when I load it and try to set a variable to it I get an error saying table expect, Got string. Here is the code
Saving
[CODE]function meta:InventorySave()
self:SetPData("Inventory" , self.inventory)
end[/CODE]
Loading
[CODE]function meta:InventoryLoad()
local inv = self:GetPData("Inventory" , -1)
if inv ~= -1 then
print("[INVENTORY] Loaded inventory for "..self:Nick())
self.inventory = inv
else
print("[INVENTORY] Created PData for "..self:Nick())
self.inventory = {}
self.inventory[1] = {}
self.inventory[1].itemID = 1
self.inventory[1].amount = 2
end
end[/CODE]
The error is on the line with "self.inventory = inv"
The error is
[ERROR] gamemodes/gmodz/gamemode/inventory/sv_inventory.lua:84: bad key to string index (number expected, got string)
1. error - [C]:-1
2. __index - lua/includes/extensions/string.lua:310
3. InventoryLoad - gamemodes/gmodz/gamemode/inventory/sv_inventory.lua:84
4. v - gamemodes/gmodz/gamemode/inventory/sv_inventory.lua:17
5. unknown - lua/includes/modules/hook.lua:84
PData uses the SQL database of the server or client. The column in the PData SQL table is set to the data type TEXT, a string type, and GetPData makes no effort to convert it. Therefore, if your data is not a string, you need to convert it back to whatever type you want to use. With tables, you could use [URL="http://wiki.garrysmod.com/page/table/concat"]table.concat[/URL] to convert the table into one string and then save that using PData. When you try to retrieve it, simply use [URL="http://wiki.garrysmod.com/page/string/Explode"]string.Explode[/URL] to convert it back into a table. You will still need to use tonumber to convert the strings in a table back to a number that you can use for arithmetic. You could also use [URL="http://wiki.garrysmod.com/page/util/TableToJSON"]util.TableToJSON[/URL] to convert the table into a JSON string and then save that to the database. When you want to get the PData, just use [URL="http://wiki.garrysmod.com/page/util/JSONToTable"]util.JSONToTable[/URL] to convert the JSON back into a table.
Edit:
Yeah, table.concat won't work with this table because there are tables within the main table. Use util.TableToJSON instead.
Sorry, you need to Log In to post a reply to this thread.