Advertisement
Guest User

inventory system example (Roblox)

a guest
Jun 14th, 2017
1,826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.69 KB | None | 0 0
  1. local inventory = {}
  2.  
  3. local function addItems(player, itemName)
  4.     if type(inventory[player]) == "table" then
  5.         inventory[player][#inventory[player]+1] = itemName
  6.     else
  7.         inventory[player] = {}
  8.         inventory[player][#inventory[player]+1] = itemName
  9.     end
  10. end
  11.  
  12. local function removeItems(player, itemName)
  13.     if type(inventory[player]) == "table" then
  14.         for k,v in pairs(inventory[player]) do
  15.             if v == itemName then
  16.                 inventory[player][k] = nil -- if this doesnt work, do:
  17.                 -- inventory[player][itemName] = nil
  18.                 break
  19.             end
  20.         end
  21.     end
  22. end
  23.  
  24. local DS = game:GetService("DataStoreService")
  25. local inventoryStore = DS:GetDataStore("Inventories")
  26.  
  27. local function loadInventory(player)
  28.     if player.UserId < 1 then return end -- dont load or save for guests.
  29.     local success, loaded = pcall(function()
  30.         local didLoad = false
  31.         local getId = tonumber(player.UserId) or 0
  32.         inventoryStore:UpdateAsync("user-"..getId,function(storedData)
  33.             local manageData = storedData or {}
  34.             inventory[player] = manageData
  35.             didLoad = true
  36.             return manageData
  37.         end)
  38.         return didLoad
  39.     end)
  40.     if success and loaded then
  41.         print("Successfully loaded inventory for: "..tostring(player))
  42.     else
  43.         warn("Error loading inventory for: "..tostring(player))
  44.     end
  45. end
  46.  
  47. local function saveInventory(player)
  48.     if player.UserId < 1 then return end -- dont load or save for guests.
  49.     local success, saved = pcall(function()
  50.         local didSave = false
  51.         local getId = tonumber(player.UserId) or 0
  52.         inventoryStore:UpdateAsync("user-"..getId,function(storedData)
  53.             local manageData = storedData or {}
  54.             manageData = inventory[player]
  55.             didSave = true
  56.             return manageData
  57.         end)
  58.         return didSave
  59.     end)
  60.     if success and saved then
  61.         print("Successfully saved inventory for: "..tostring(player))
  62.     else
  63.         warn("Error saving inventory for: "..tostring(player))
  64.     end
  65. end
  66.  
  67. local itemStorage = game.ServerStorage.DataType
  68.  
  69. local function applyInventory(player)
  70.     if type(inventory[player]) == "table" then
  71.         for k,v in pairs(inventory[player]) do
  72.             if itemStorage:FindFirstChild(v) then
  73.                 itemStorage[v]:Clone().Parent = player.Backpack
  74.             end
  75.         end
  76.     end
  77. end
  78.  
  79. game.Players.PlayerAdded:Connect(function(player)
  80.     inventory[player] = {}
  81.     loadInventory(player)
  82.     applyInventory(player)
  83. end)
  84.  
  85. game.Players.PlayerRemoving:Connect(function(player)
  86.     saveInventory(player)
  87. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement