So I'm working on a skin shop in my game. But for some reason my Data Store will not save my table that I've created with the object models of the skins. Here's my error message
DataStore Error: 35: invalid argument #1 to 'pairs' (table expected, got string)
local dataStoreService = game:GetService("DataStoreService") local TokensDataStore = dataStoreService:GetDataStore("Tokens") local WinsDataStore = dataStoreService:GetDataStore("Wins") local InventoryDataStore = dataStoreService:GetDataStore("Inventory") local http = game:GetService("HttpService") game.Players.PlayerAdded:Connect(function(plr) local leaderstats = Instance.new("Folder",plr) leaderstats.Name = "leaderstats" local Wins = Instance.new("IntValue",leaderstats) Wins.Name = "Wins" local Tokens = Instance.new("IntValue",leaderstats) Tokens.Name = "Tokens" local Plr = Instance.new("Folder",game.ServerStorage:WaitForChild("PlayerInventory")) Plr.Name = plr.Name local WinsData local TokensData local InventoryData local success,errormessage = pcall(function() WinsData = WinsDataStore:GetAsync(plr.UserId.."-Wins") TokensData = TokensDataStore:GetAsync(plr.UserId.."-Tokens") InventoryData = InventoryDataStore:GetAsync(plr.UserId.."-Inventory") end) if success then Tokens.Value = TokensData print(TokensData) Wins.Value = WinsData print(WinsData) if InventoryData then print(InventoryData) print(tostring(InventoryData)) for i,skin in pairs(http:JSONDecode(tostring(InventoryData))) do print(skin.Name..": hi") local playerInventory = game.ServerStorage:WaitForChild("PlayerInventory") end end print("Loaded data.") else warn("DataStore error: ",errormessage) end end) game.ReplicatedStorage.events.ServerTokenUI.OnServerEvent:Connect(function(plr) local tokens = plr.leaderstats.Tokens game.ReplicatedStorage.events.ClientTokenUI:FireClient(plr,tokens) end) local bindableEvent = Instance.new("BindableEvent") game.Players.PlayerRemoving:Connect(function(plr) local success,errormessage = pcall(function() WinsDataStore:SetAsync(plr.UserId.."-Wins",plr.leaderstats.Wins.Value) TokensDataStore:SetAsync(plr.UserId.."-Tokens",plr.leaderstats.Tokens.Value) local skins = {} local inventory = game.ServerStorage.PlayerInventory[plr.Name] for _,skin in pairs(game.ServerStorage:WaitForChild("Skins"):GetChildren()) do if inventory:FindFirstChild(skin.Name) then table.insert(skins,skin) print("Added: "..skin.Name) end print(skin) end InventoryDataStore:SetAsync(plr.UserId.."-Inventory",http:JSONEncode(skins)) if #game.Players:GetPlayers() <= 1 then bindableEvent:Fire() end end) if success then print("Saved data.") else warn("DataStore Error: ",errormessage) end end) game:BindToClose(function() bindableEvent.Event:Wait() end)
Hi! Sorry, I'm not the best at answering questions but I hope this helps!
Datastores can only store characters, not objects. If you want to store a list of objects, try using HTTPService so you can convert the table into a string with HTTPService:JsonEncode(table)
When you want to turn the string back into a table, you can then do HTTPService:JsonDecode(string).
Hope this helps!