Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DataStoreService = game:GetService("DataStoreService")
- local InventoryStore = DataStoreService:GetDataStore("Inventory_v1")
- local InventoryEvent = game.ReplicatedStorage:WaitForChild("Remotes").Inventory
- _G.playerInventories = {}
- local saveLog = {}
- game.Players.PlayerAdded:Connect(function(Player)
- if InventoryStore:GetAsync(Player.UserId) == nil then
- _G.playerInventories[Player] = { Weapons = {};
- Hats = {};
- BucketFaces = {};
- Particles = {};
- EquippedWeapon = {Name = "Katana"; Type = "Weapon"};
- EquippedHat = "None";
- EquippedBucketFace = "None";
- EquippedParticle = "None";
- }
- _G:Equip(Player,"currentWeapon")
- InventoryEvent:FireClient(Player,"Refresh",_G.playerInventories[Player])
- else
- _G.playerInventories[Player] = game:GetService("HttpService"):JSONDecode(InventoryStore:GetAsync(Player.UserId))
- _G:Equip(Player,"currentWeapon")
- InventoryEvent:FireClient(Player,"Refresh",_G.playerInventories[Player])
- end
- saveLog[Player] = true
- end)
- game.Players.PlayerRemoving:Connect(function(Player)
- pcall(function()
- InventoryStore:SetAsync(Player.UserId,game:GetService("HttpService"):JSONEncode(_G.playerInventories[Player]))
- _G.playerInventories[Player] = nil
- saveLog[Player] = false
- end)
- end)
- function _G:Add(Player,Item)
- if _G.playerInventories[Player] ~= nil then
- if _G.playerInventories[Player][Item.Type.."s"] ~= nil then
- table.insert(_G.playerInventories[Player][Item.Type.."s"],Item)
- end
- InventoryEvent:FireClient(Player,"Refresh",_G.playerInventories[Player])
- end
- end
- function _G:Delete(Player,Item)
- if _G.playerInventories[Player] ~= nil then
- if _G.playerInventories[Player][Item.Type.."s"] ~= nil then
- for x, inventoryItem in pairs(_G.playerInventories[Player][Item.Type.."s"]) do
- if inventoryItem.Name == Item.Name and inventoryItem.Type == Item.Type then
- table.remove(_G.playerInventories[Player][Item.Type.."s"],x)
- break
- end
- end
- end
- InventoryEvent:FireClient(Player,"Refresh",_G.playerInventories[Player])
- end
- end
- function _G:Equip(Player,Item)
- if _G.playerInventories[Player] ~= nil then
- if type(Item) ~= "string" then
- if _G.playerInventories[Player]["Equipped"..Item.Type] ~= nil then
- _G:Unequip(Player,Item)
- -- Check what item type it is, and then make it equip function with if statement (if face then make it change decal etc...)
- if Item.Type == "Weapon" then
- if game.ReplicatedStorage.Tools:FindFirstChild(Item.Name) then
- repeat wait() until Player.Character ~= nil
- Player.Character.Humanoid:UnequipTools()
- Player.Backpack:ClearAllChildren()
- game.ReplicatedStorage.Tools:FindFirstChild(Item.Name):Clone().Parent = Player.Backpack
- end
- end
- _G.playerInventories[Player]["Equipped"..Item.Type] = Item
- end
- else
- if string.match(Item,"current") ~= nil then
- local returned = string.gsub(Item,"current","")
- if _G.playerInventories[Player]["Equipped"..returned] ~= nil then
- if Item == "Weapon" then
- if game.ReplicatedStorage.Tools:FindFirstChild(_G.playerInventories[Player]["Equipped"..returned].Name) then
- repeat wait() until Player.Character ~= nil
- Player.Character.Humanoid:UnequipTools()
- Player.Backpack:ClearAllChildren()
- game.ReplicatedStorage.Tools:FindFirstChild(_G.playerInventories[Player]["Equipped"..returned].Name):Clone().Parent = Player.Backpack
- end
- end
- end
- end
- end
- end
- end
- function _G:Unequip(Player,Item)
- if _G.playerInventories[Player] ~= nil then
- if _G.playerInventories[Player]["Equipped"..Item.Type] ~= nil then
- if _G.playerInventories[Player]["Equipped"..Item.Type].Name == Item.Name and _G.playerInventories[Player]["Equipped"..Item.Type].Type == Item.Type then
- _G.Add(Player,Item)
- _G.playerInventories[Player]["Equipped"..Item.Type] = "None"
- if Item.Type == "Weapon" then
- repeat wait() until Player.Character ~= nil
- Player.Character.Humanoid:UnequipTools()
- Player.Backpack:ClearAllChildren()
- end
- end
- end
- end
- end
- InventoryEvent.OnServerEvent:connect(function(Player,Action,...)
- local Args = {...}
- if Action == "Equip" then
- if Args[1] ~= nil then
- _G:Equip(Player,Args[1])
- end
- elseif Action == "Unequip" then
- if Args[1] ~= nil then
- _G:Unequip(Player,Args[1])
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement