Advertisement
Quoteory

Advanced Inventory Lua

Sep 12th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.75 KB | None | 0 0
  1. -- Inventory Service
  2. -- Quoteory
  3. -- September 12, 2019
  4.  
  5. --[[
  6.    
  7.     Server:
  8.        
  9.    
  10.  
  11.  
  12.     Client:
  13.        
  14.    
  15.  
  16. --]]
  17.  
  18. local IDList
  19. local random = Random.new()
  20. local randomMin, randomMax = 100, 99999
  21.  
  22. local InventoryService = {Client = {}}
  23.  
  24. function InventoryService.getRandomNumber()
  25.     return random:NextInteger(randomMin, randomMax)
  26. end
  27.  
  28. function InventoryService:GetUniqueSerial(Inventory)
  29.    
  30.     local armors = Inventory["Armors"]
  31.     local weapons = Inventory["Weapons"]
  32.    
  33.     local uniqueSerial
  34.    
  35.     local function recursion()
  36.         uniqueSerial = self.getRandomNumber()
  37.         if armors[uniqueSerial] or weapons[uniqueSerial] then
  38.             recursion()
  39.         end
  40.     end
  41.    
  42.     recursion()
  43.    
  44.     return uniqueSerial
  45.    
  46. end
  47.  
  48. function InventoryService:GetOwnedItem(Inventory, Serial)
  49.     local armors = Inventory["Armors"]
  50.     local weapons = Inventory["Weapons"]
  51.    
  52.     local armorGet = armors[Serial]
  53.     local weaponGet = weapons[Serial]
  54.    
  55.     if weaponGet then
  56.         return weapons, weaponGet
  57.     elseif armorGet then
  58.         return armors, armorGet
  59.     end
  60.    
  61. end
  62.  
  63. function InventoryService:AddItem(Inventory, ID)
  64.    
  65.     local armors = Inventory["Armors"]
  66.     local weapons = Inventory["Weapons"]
  67.     local item = IDList[ID]
  68.    
  69.     if not item then return end
  70.    
  71.     if item.Type == "Weapon" then
  72.         weapons[self:GetUniqueSerial(Inventory)] = {ID = ID, Quantity = 1}
  73.         print("Added item")
  74.     else
  75.         print("Not weapon type")
  76.     end
  77.    
  78. end
  79.  
  80. function InventoryService:RemoveItem(Inventory, Serial)
  81.    
  82.     local itemInventory, item = self:GetOwnedItem(Inventory, Serial)
  83.     if itemInventory and item then
  84.         itemInventory[Serial] = nil
  85.         print("Removed " .. Serial)
  86.     end
  87.    
  88. end
  89.  
  90. function InventoryService:Start()
  91.    
  92. end
  93.  
  94.  
  95. function InventoryService:Init()
  96.     IDList = self.Shared.IDList
  97.  
  98. end
  99.  
  100.  
  101. return InventoryService
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement