Working with the Web API for Inventory but I came across that if they’re inventory is private it errors;
Images
Private Inventory
Public Inventory
Is there a way that I can tell if it’s private so I can put a TextLabel saying “User inventory is private.”?
What I tried to do is do something with the error code and that works, but if the inventory is public then that also errors because obviously ["errors"] doesn’t exist in Data.
local Data = {}
local succ1, res1 = pcall(function()
if Data then
if Data["errors"][1]["code"] == 4 then
return true
else
return false
end
end
end)
if res1 then
script.Parent.Folder.InventoryPrivate.Visible = true
end
local succ2, res2 = pcall(function()
if Data then
if #Data["data"] == 0 then
return true
else
return false
end
end
end)
if res2 then
script.Parent.Folder.NoResultsFound.Visible = true
end
Just make sure “Data” is always defined you can do that by adding or {} at the end of the statement which assigns a value to it.
local Data = {}
local DataError = Data["errors"]
if DataError then
local errCode = Data["errors"][1]["code"]
if errCode == 4 then
--do something
end
end
local DataData = Data["data"]
if DataData then
if #DataData == 0 then
--do something
end
end
Ignore the first block of code I provided, this second one is working (tested in Studio).
Thanks but I’ve run into another problem. (Idk if this is connected)
Line 34: attempt to get length of a nil value
Script
local ProxyService = require(game:GetService("ServerScriptService").ProxyService)
local Proxy = ProxyService:New('<MY_KEY>')
local HttpService = game:GetService("HttpService")
game.ReplicatedStorage.UserIdEvent.OnServerEvent:Connect(function(player, id)
local Response = Proxy:Get("https://inventory.roblox.com/v2/users/"..id.."/inventory?assetTypes=Hat&limit=100&sortOrder=Desc").body
local Data = HttpService:JSONDecode(Response)
local i = 1
warn(Data)
local Data = {}
local DataError = Data["errors"]
if DataError then
local errCode = Data["errors"][1]["code"]
if errCode == 4 then
script.Parent.Folder.InventoryPrivate.Visible = true
end
end
local DataData = Data["data"]
if DataData then
if #DataData == 0 then
script.Parent.Folder.NoResultsFound.Visible = true
end
end
local Count = #Data["data"] --THIS IS LINE 34
local Items = Data["data"]
while i < Count+1 do
local Clone = game.ServerStorage.ItemCard:Clone()
Clone.Title.Text = Items[i]["name"]
local itemid = Items[i]["assetId"]
Clone.Icon.Image = "https://www.roblox.com/asset-thumbnail/image?assetId="..itemid.."&width=420&height=420&format=png"
Clone.Parent = script.Parent
script.Parent.Folder.ItemsCount.Text = "Accessories > Hat ("..i..")"
if i == 100 then
script.Parent.Folder.ItemsCount.Text = "Accessories > Hat ("..i..") | MAX: 100 (Only shows the 100 most recent items)"
break
end
i = i + 1
end
end)
local ProxyService = require(game:GetService("ServerScriptService").ProxyService)
local Proxy = ProxyService:New('<MY_KEY>')
local HttpService = game:GetService("HttpService")
game.ReplicatedStorage.UserIdEvent.OnServerEvent:Connect(function(player, id)
local Response = Proxy:Get("https://inventory.roblox.com/v2/users/"..id.."/inventory?assetTypes=Hat&limit=100&sortOrder=Desc").body
local Data = HttpService:JSONDecode(Response)
local i = 1
warn(Data)
local Data = {}
local DataError = Data["errors"]
if DataError then
local errCode = Data["errors"][1]["code"]
if errCode == 4 then
script.Parent.Folder.InventoryPrivate.Visible = true
end
end
local DataData = Data["data"]
if DataData then
if #DataData == 0 then
script.Parent.Folder.NoResultsFound.Visible = true
end
end
local DataData2 = Data["data"]
local countData
if DataData2 then
countData = #DataData2
end
local Items = Data["data"]
while i < countData+1 do
local Clone = game.ServerStorage.ItemCard:Clone()
Clone.Title.Text = Items[i]["name"]
local itemid = Items[i]["assetId"]
Clone.Icon.Image = "https://www.roblox.com/asset-thumbnail/image?assetId="..itemid.."&width=420&height=420&format=png"
Clone.Parent = script.Parent
script.Parent.Folder.ItemsCount.Text = "Accessories > Hat ("..i..")"
if i == 100 then
script.Parent.Folder.ItemsCount.Text = "Accessories > Hat ("..i..") | MAX: 100 (Only shows the 100 most recent items)"
break
end
i = i + 1
end
end)
local ProxyService = require(game:GetService("ServerScriptService").ProxyService)
local Proxy = ProxyService:New('<MY_KEY>')
local HttpService = game:GetService("HttpService")
game.ReplicatedStorage.UserIdEvent.OnServerEvent:Connect(function(player, id)
local Response = Proxy:Get("https://inventory.roblox.com/v2/users/"..id.."/inventory?assetTypes=Hat&limit=100&sortOrder=Desc").body
local Data = HttpService:JSONDecode(Response)
local i = 1
warn(Data)
local Data = {}
local DataError = Data["errors"]
if DataError then
local errCode = Data["errors"][1]["code"]
if errCode == 4 then
script.Parent.Folder.InventoryPrivate.Visible = true
end
end
local DataData = Data["data"]
if DataData then
if #DataData == 0 then
script.Parent.Folder.NoResultsFound.Visible = true
end
end
local DataData2 = Data["data"]
local countData
if DataData2 then
countData = #DataData2
end
local Items = Data["data"]
if countData then
while i < countData+1 do
local Clone = game.ServerStorage.ItemCard:Clone()
Clone.Title.Text = Items[i]["name"]
local itemid = Items[i]["assetId"]
Clone.Icon.Image = "https://www.roblox.com/asset-thumbnail/image?assetId="..itemid.."&width=420&height=420&format=png"
Clone.Parent = script.Parent
script.Parent.Folder.ItemsCount.Text = "Accessories > Hat ("..i..")"
if i == 100 then
script.Parent.Folder.ItemsCount.Text = "Accessories > Hat ("..i..") | MAX: 100 (Only shows the 100 most recent items)"
break
end
i = i + 1
end
end
end)