Hello! im trying to use a script where the inventory of someone saves when they leave the game and rejoin again. i seem to get the error ServerScriptService.ToolSaving:9: bad argument #1 to 'pairs' (table expected, got nil) (line 9) Does anyone know what the problem is? Thanks!

local Data = game:GetService("DataStoreService"):GetDataStore("Inventory")

game.Players.PlayerAdded:Connect(function(plr)

 local SavedStuff = Data:GetAsync(plr.userId)

 if SavedStuff == nil then

  for i,v in pairs(SavedStuff)do

   if game.ServerStorage.Inventory:FindFirstChild(v)~= nil then

    local Weapon = game.ServerStorage.Inventory:FindFirstChild(v):Clone()

             Weapon.Parent = plr:WaitForChild("Backpack")

          end

         end

 end

 plr.CharacterRemoving:Connect(function()

  plr.Character.Humanoid:UnequipTools()

 end)

end)



game.Players.PlayerRemoving:Connect(function(plr)

 local Table = {}

 for i,v in pairs(plr.Backpack:GetChildren())do

  table.insert(Table,v.Name)

 end

 if Table ~= nil then

     Data:SetAsync(plr.userId,Table)

 end

end)

2 comments

0

not sure if this is case sensitive or not but try using UserId on line 5 (plr.UserId)

0

"if SavedStuff == nil then" - This code is only running if SavedStuff is nil. You're then trying to go through the values in SavedStuff, which doesn't exist as it is nil. The fact that it is nil suggests no data has been saved, which makes sense. Try to do if SavedStuff ~= nil then