Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ## SERVICES ## --
- local DataStoreService = game:GetService("DataStoreService")
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local HttpService = game:GetService("HttpService")
- local ServerStorage = game:GetService("ServerStorage")
- local MarketPlaceService = game:GetService("MarketplaceService")
- -- ## VARIABLES ## --
- local Backpacks = {} :: { [Player]: { Tool } }
- local IdToTool = {} :: { [string]: Tool }
- local InvRem = ReplicatedStorage.InvRem
- local BackpacksFolder = Instance.new("Folder")
- BackpacksFolder.Parent = ServerStorage
- -- ## FUNCTIONS ## --
- local function len(t: {}): number
- local c = 0
- for _ in t do
- c += 1
- end
- return c
- end
- local function validatePlayerInventory(player: Player, inventory: Inventory): boolean
- local backpack = player:FindFirstChild("Backpack")
- local character = player.Character
- if not backpack then
- return false
- end
- for _, tool in inventory :: { Tool } do
- if tool.Parent ~= backpack and not character and tool.Parent ~= character then
- print(`Tool of name {tool} shouldn't be in {player}'s inventory.`)
- print(backpack:GetChildren())
- print(tool:GetFullName())
- return false
- end
- end
- return true
- end
- local function getClone(player: Player, tool: Tool): Tool?
- local backpackCopy = BackpacksFolder:FindFirstChild(tostring(player.UserId))
- if not backpackCopy then
- return nil
- elseif not tool:GetAttribute("Id") then
- return nil
- end
- return backpackCopy:FindFirstChild(tool:GetAttribute("Id"))
- end
- local function createClone(player: Player, tool: Tool)
- local clone = getClone(player, tool)
- if clone then
- for attribute, value in tool:GetAttributes() do
- clone:SetAttribute(attribute, value)
- end
- return
- end
- local backpack = player:FindFirstChild("Backpack") :: Backpack
- local backpackCopy = BackpacksFolder:FindFirstChild(tostring(player.UserId))
- if not backpackCopy then
- return
- end
- clone = tool:Clone()
- clone.Name = tool:GetAttribute("Id")
- clone.Parent = backpackCopy
- clone:SetAttribute("Name", tool.Name)
- end
- local function deleteClone(player: Player, tool: Tool)
- local clone = getClone(player, tool)
- if clone then
- clone:Destroy()
- end
- end
- local function fillBackpack(player: Player)
- local backpack = player:FindFirstChildOfClass("Backpack") :: Backpack
- local backpackCopy = BackpacksFolder:FindFirstChild(tostring(player.UserId))
- local backpackTable = {}
- local disableCustomInventory = false
- if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, 00) and player.Character:FindFirstChild("ForceField") then
- disableCustomInventory = true
- end
- if not backpackCopy then
- backpackCopy = Instance.new("Folder")
- backpackCopy.Name = tostring(player.UserId)
- backpackCopy.Parent = BackpacksFolder
- end
- if #backpackCopy:GetChildren() > 0 and #backpack:GetChildren() > 0 and not disableCustomInventory then
- backpack:ClearAllChildren()
- end
- if #backpackCopy:GetChildren() == 0 and #backpack:GetChildren() == 0 and not disableCustomInventory then
- for index, tool in ServerStorage.Tools:GetChildren() do
- local clone = tool:Clone()
- local id = HttpService:GenerateGUID(false)
- clone:SetAttribute("Id", id)
- clone:SetAttribute("Position", index)
- clone.Parent = backpack
- createClone(player, clone)
- backpackTable[tool:GetAttribute("Position") or index] = clone
- end
- elseif #backpackCopy:GetChildren() > 0 and not disableCustomInventory then
- for index, tool in backpackCopy:GetChildren() do
- local clone = tool:Clone()
- clone.Name = tool:GetAttribute("Name")
- clone.Parent = backpack
- backpackTable[tool:GetAttribute("Position") or index] = clone
- end
- elseif #backpack:GetChildren() > 0 and not disableCustomInventory then
- for _, tool in backpack:GetChildren() do
- if not tool:GetAttribute("Id") or not getClone(player, tool) then
- tool:SetAttribute("Id", HttpService:GenerateGUID(false))
- createClone(player, tool)
- end
- end
- end
- Backpacks[player] = backpackTable
- end
- -- ## EVENTS ## --
- Players.PlayerAdded:Connect(function(player: Player)
- Backpacks[player] = {}
- local backpack = player:FindFirstChildOfClass("Backpack") :: Backpack
- local backpackTable = Backpacks[player]
- local equippedTool = nil
- local backpackCopy = Instance.new("Folder")
- local connections = {}
- backpackCopy.Name = tostring(player.UserId)
- backpackCopy.Parent = BackpacksFolder
- player.CharacterAdded:Connect(function(character)
- task.defer(function()
- fillBackpack(player)
- end)
- backpack = player:FindFirstChildOfClass("Backpack")
- player.CharacterRemoving:Once(function()
- for _, connection in connections do
- connection:Disconnect()
- end
- end)
- connections[#connections + 1] = backpack.ChildAdded:Connect(function(tool: Instance)
- if not tool:IsA("Tool") then
- return
- end
- if not tool:GetAttribute("Id") then
- tool:SetAttribute("Id", HttpService:GenerateGUID(false))
- end
- createClone(player, tool :: Tool)
- table.insert(backpackTable, tool)
- end)
- connections[#connections + 1] = backpack.ChildRemoved:Connect(function(tool: Instance)
- if not tool:IsA("Tool") then
- return
- elseif player.Character and tool.Parent == player.Character then
- equippedTool = tool
- return
- end
- deleteClone(player, tool :: Tool)
- table.remove(backpackTable, table.find(backpackTable, tool))
- end)
- connections[#connections + 1] = character.ChildRemoved:Connect(function(child)
- if child ~= equippedTool then
- return
- end
- equippedTool = nil
- if child.Parent ~= backpack then
- deleteClone(player, child :: Tool)
- table.remove(backpackTable, table.find(backpackTable, child))
- end
- end)
- end)
- end)
- Players.PlayerRemoving:Connect(function(player: Player)
- Backpacks[player] = nil
- end)
- InvRem.ReorderInventory.OnServerEvent:Connect(function(player: Player, inventory: Inventory)
- if not validatePlayerInventory(player, inventory) then
- return
- end
- print(inventory)
- local newTable = {}
- for index, tool in inventory do
- index = tonumber(index)
- tool:SetAttribute("Position", index)
- createClone(player, tool)
- newTable[index] = tool
- end
- Backpacks[player] = newTable
- end)
- -- ## SETUP ## --
- for _, tool in ServerStorage.Tools:GetChildren() do
- if not tool:IsA("Tool") then
- continue
- end
- IdToTool[tool:GetAttribute("Id") or tool.Name] = tool
- end
- -- ## TYPES ## --
- type Inventory = { [number]: Tool }
- type EncodedInventory = { [number]: string }
Advertisement
Add Comment
Please, Sign In to add comment