Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.47 KB | None | 0 0
  1.  
  2.  
  3. local DataStoreService = game:GetService("DataStoreService")
  4. local InventoryStore = DataStoreService:GetDataStore("Inventory_v1")
  5. local InventoryEvent = game.ReplicatedStorage:WaitForChild("Remotes").Inventory
  6.  
  7. _G.playerInventories = {}
  8.  
  9.  
  10. local saveLog = {}
  11. game.Players.PlayerAdded:Connect(function(Player)
  12.     if InventoryStore:GetAsync(Player.UserId) == nil then
  13.         _G.playerInventories[Player] = {    Weapons          = {};
  14.                                             Hats             = {};
  15.                                             BucketFaces      = {};
  16.                                             Particles        = {};
  17.                                             EquippedWeapon   = {Name = "Katana"; Type = "Weapon"};
  18.                                             EquippedHat      = "None";
  19.                                             EquippedBucketFace     = "None";
  20.                                             EquippedParticle = "None";
  21.                                         }
  22.         _G:Equip(Player,"currentWeapon")
  23.         InventoryEvent:FireClient(Player,"Refresh",_G.playerInventories[Player])
  24.     else
  25.         _G.playerInventories[Player] = game:GetService("HttpService"):JSONDecode(InventoryStore:GetAsync(Player.UserId))
  26.         _G:Equip(Player,"currentWeapon")
  27.         InventoryEvent:FireClient(Player,"Refresh",_G.playerInventories[Player])
  28.     end
  29.    
  30.     saveLog[Player] = true
  31.  
  32. end)
  33.  
  34. game.Players.PlayerRemoving:Connect(function(Player)
  35.     pcall(function()
  36.     InventoryStore:SetAsync(Player.UserId,game:GetService("HttpService"):JSONEncode(_G.playerInventories[Player]))
  37.    
  38.     _G.playerInventories[Player] = nil
  39.     saveLog[Player] = false
  40.     end)
  41. end)
  42.  
  43. function _G:Add(Player,Item)
  44.     if _G.playerInventories[Player] ~= nil then
  45.         if _G.playerInventories[Player][Item.Type.."s"] ~= nil then
  46.             table.insert(_G.playerInventories[Player][Item.Type.."s"],Item)
  47.         end
  48.         InventoryEvent:FireClient(Player,"Refresh",_G.playerInventories[Player])
  49.     end
  50. end
  51.  
  52. function _G:Delete(Player,Item)
  53.     if _G.playerInventories[Player] ~= nil then
  54.         if _G.playerInventories[Player][Item.Type.."s"] ~= nil then
  55.             for x, inventoryItem in pairs(_G.playerInventories[Player][Item.Type.."s"]) do
  56.                 if inventoryItem.Name == Item.Name and inventoryItem.Type == Item.Type then
  57.                     table.remove(_G.playerInventories[Player][Item.Type.."s"],x)
  58.                     break
  59.                 end
  60.             end
  61.         end
  62.         InventoryEvent:FireClient(Player,"Refresh",_G.playerInventories[Player])
  63.     end
  64. end
  65.  
  66. function _G:Equip(Player,Item)
  67.     if _G.playerInventories[Player] ~= nil then
  68.         if type(Item) ~= "string" then
  69.             if _G.playerInventories[Player]["Equipped"..Item.Type] ~= nil then
  70.                 _G:Unequip(Player,Item)
  71.                 -- Check what item type it is, and then make it equip function with if statement (if face then make it change decal etc...)
  72.                 if Item.Type == "Weapon" then
  73.                     if game.ReplicatedStorage.Tools:FindFirstChild(Item.Name) then
  74.                         repeat wait() until Player.Character ~= nil
  75.                         Player.Character.Humanoid:UnequipTools()
  76.                         Player.Backpack:ClearAllChildren()
  77.                         game.ReplicatedStorage.Tools:FindFirstChild(Item.Name):Clone().Parent = Player.Backpack
  78.                     end
  79.                 end
  80.                 _G.playerInventories[Player]["Equipped"..Item.Type] = Item
  81.             end
  82.         else
  83.             if string.match(Item,"current") ~= nil then
  84.                 local returned = string.gsub(Item,"current","")
  85.                 if _G.playerInventories[Player]["Equipped"..returned] ~= nil then
  86.                     if Item == "Weapon" then
  87.                         if game.ReplicatedStorage.Tools:FindFirstChild(_G.playerInventories[Player]["Equipped"..returned].Name) then
  88.                             repeat wait() until Player.Character ~= nil
  89.                             Player.Character.Humanoid:UnequipTools()
  90.                             Player.Backpack:ClearAllChildren()
  91.                             game.ReplicatedStorage.Tools:FindFirstChild(_G.playerInventories[Player]["Equipped"..returned].Name):Clone().Parent = Player.Backpack
  92.                         end
  93.                     end
  94.                 end
  95.             end
  96.         end
  97.     end
  98. end
  99.            
  100. function _G:Unequip(Player,Item)
  101.     if _G.playerInventories[Player] ~= nil then
  102.         if _G.playerInventories[Player]["Equipped"..Item.Type] ~= nil then
  103.             if _G.playerInventories[Player]["Equipped"..Item.Type].Name == Item.Name and _G.playerInventories[Player]["Equipped"..Item.Type].Type == Item.Type then
  104.                 _G.Add(Player,Item)
  105.                 _G.playerInventories[Player]["Equipped"..Item.Type] = "None"
  106.                 if Item.Type == "Weapon" then
  107.                     repeat wait() until Player.Character ~= nil
  108.                     Player.Character.Humanoid:UnequipTools()
  109.                     Player.Backpack:ClearAllChildren() 
  110.                 end
  111.             end
  112.         end
  113.     end
  114. end
  115.            
  116.            
  117.  
  118. InventoryEvent.OnServerEvent:connect(function(Player,Action,...)
  119.     local Args = {...}
  120.     if Action == "Equip" then
  121.         if Args[1] ~= nil then
  122.             _G:Equip(Player,Args[1])
  123.         end
  124.     elseif Action == "Unequip" then
  125.         if Args[1] ~= nil then
  126.             _G:Unequip(Player,Args[1])
  127.         end
  128.     end
  129. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement