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)

9 comments
when I loaded all of the data i got an error - JsonDecode is not a valid member of HttpService "HttpService"
sorry meant HTTPService:JSONDecode! json in all caps! Try checking the documentation https://developer.roblox.com/en-us/api-reference/class/HttpService
okay, thanks! I'll see if this works
ServerScriptService.leaderstats:35: invalid argument #1 to 'pairs' (table expected, got string)
I edited the script to show you what i did
try to remove the tostring
and also try to and see if it works: print(http:JSONDecode(tostring(InventoryData)))
i did
it prints an empty table, so i don't think it saved