What I’m confused about is why this errors when it checks if the data is not nil on the line before.
players.PlayerAdded:Connect(function(plr)
-- leaderstats
leaderstats = servStorage.leaderstats
leaderstats.Parent = plr
--loading data
-- Inventory Data
local success, inventoryData = pcall(function()
return invStore:GetAsync(plr.UserId)
end)
if success then
if inventoryData ~= nil then
for i, v in pairs(inventoryData["Ore"]) do -- this is the line with the error
leaderstats.Ore[i].Value = v
end
for i, v in pairs(inventoryData["Materials"]) do
leaderstats.Materials[i].Value = v
end
end
end
-- Leaderstat Data
local success, leaderstatData = pcall(function()
return invStore:GetAsync(plr.UserId)
end)
if success then
if leaderstatData ~= nil then
for i, v in pairs(leaderstatData) do
leaderstats[i].Value = v
end
end
end
end)
You have the option to split that if statement if needed so that, every for loop is wrapped by a single statement.
if inventoryData ~= nil then
if inventoryData["Ore"] then
for i, v in pairs(inventoryData["Ore"]) do
leaderstats.Ore[i].Value = v
end
end
if inventoryData["Materials"] then
for i, v in pairs(inventoryData["Materials"]) do
leaderstats.Materials[i].Value = v
end
end
end