DevKxnq1221

GPTPaste

Apr 1st, 2023
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. -- ## SERVICES ## --
  2.  
  3. local DataStoreService = game:GetService("DataStoreService")
  4. local Players = game:GetService("Players")
  5. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  6. local HttpService = game:GetService("HttpService")
  7. local ServerStorage = game:GetService("ServerStorage")
  8. local MarketPlaceService = game:GetService("MarketplaceService")
  9.  
  10. -- ## VARIABLES ## --
  11.  
  12. local Backpacks = {} :: { [Player]: { Tool } }
  13. local IdToTool = {} :: { [string]: Tool }
  14.  
  15. local InvRem = ReplicatedStorage.InvRem
  16. local BackpacksFolder = Instance.new("Folder")
  17.  
  18. BackpacksFolder.Parent = ServerStorage
  19.  
  20. -- ## FUNCTIONS ## --
  21.  
  22. local function len(t: {}): number
  23. local c = 0
  24.  
  25. for _ in t do
  26. c += 1
  27. end
  28.  
  29. return c
  30. end
  31.  
  32. local function validatePlayerInventory(player: Player, inventory: Inventory): boolean
  33. local backpack = player:FindFirstChild("Backpack")
  34. local character = player.Character
  35.  
  36. if not backpack then
  37. return false
  38. end
  39.  
  40. for _, tool in inventory :: { Tool } do
  41. if tool.Parent ~= backpack and not character and tool.Parent ~= character then
  42. print(`Tool of name {tool} shouldn't be in {player}'s inventory.`)
  43. print(backpack:GetChildren())
  44. print(tool:GetFullName())
  45. return false
  46. end
  47. end
  48.  
  49. return true
  50. end
  51.  
  52. local function getClone(player: Player, tool: Tool): Tool?
  53. local backpackCopy = BackpacksFolder:FindFirstChild(tostring(player.UserId))
  54.  
  55. if not backpackCopy then
  56. return nil
  57. elseif not tool:GetAttribute("Id") then
  58. return nil
  59. end
  60.  
  61. return backpackCopy:FindFirstChild(tool:GetAttribute("Id"))
  62. end
  63.  
  64. local function createClone(player: Player, tool: Tool)
  65. local clone = getClone(player, tool)
  66.  
  67. if clone then
  68. for attribute, value in tool:GetAttributes() do
  69. clone:SetAttribute(attribute, value)
  70. end
  71.  
  72. return
  73. end
  74.  
  75. local backpack = player:FindFirstChild("Backpack") :: Backpack
  76. local backpackCopy = BackpacksFolder:FindFirstChild(tostring(player.UserId))
  77.  
  78. if not backpackCopy then
  79. return
  80. end
  81.  
  82. clone = tool:Clone()
  83.  
  84. clone.Name = tool:GetAttribute("Id")
  85. clone.Parent = backpackCopy
  86.  
  87. clone:SetAttribute("Name", tool.Name)
  88. end
  89.  
  90. local function deleteClone(player: Player, tool: Tool)
  91. local clone = getClone(player, tool)
  92.  
  93. if clone then
  94. clone:Destroy()
  95. end
  96. end
  97.  
  98. local function fillBackpack(player: Player)
  99. local backpack = player:FindFirstChildOfClass("Backpack") :: Backpack
  100. local backpackCopy = BackpacksFolder:FindFirstChild(tostring(player.UserId))
  101.  
  102. local backpackTable = {}
  103. local disableCustomInventory = false
  104.  
  105. if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, 00) and player.Character:FindFirstChild("ForceField") then
  106. disableCustomInventory = true
  107. end
  108.  
  109. if not backpackCopy then
  110. backpackCopy = Instance.new("Folder")
  111.  
  112. backpackCopy.Name = tostring(player.UserId)
  113. backpackCopy.Parent = BackpacksFolder
  114. end
  115.  
  116. if #backpackCopy:GetChildren() > 0 and #backpack:GetChildren() > 0 and not disableCustomInventory then
  117. backpack:ClearAllChildren()
  118. end
  119.  
  120. if #backpackCopy:GetChildren() == 0 and #backpack:GetChildren() == 0 and not disableCustomInventory then
  121. for index, tool in ServerStorage.Tools:GetChildren() do
  122. local clone = tool:Clone()
  123. local id = HttpService:GenerateGUID(false)
  124.  
  125. clone:SetAttribute("Id", id)
  126. clone:SetAttribute("Position", index)
  127.  
  128. clone.Parent = backpack
  129. createClone(player, clone)
  130.  
  131. backpackTable[tool:GetAttribute("Position") or index] = clone
  132. end
  133. elseif #backpackCopy:GetChildren() > 0 and not disableCustomInventory then
  134. for index, tool in backpackCopy:GetChildren() do
  135. local clone = tool:Clone()
  136.  
  137. clone.Name = tool:GetAttribute("Name")
  138. clone.Parent = backpack
  139.  
  140. backpackTable[tool:GetAttribute("Position") or index] = clone
  141. end
  142. elseif #backpack:GetChildren() > 0 and not disableCustomInventory then
  143. for _, tool in backpack:GetChildren() do
  144. if not tool:GetAttribute("Id") or not getClone(player, tool) then
  145. tool:SetAttribute("Id", HttpService:GenerateGUID(false))
  146. createClone(player, tool)
  147. end
  148. end
  149. end
  150.  
  151. Backpacks[player] = backpackTable
  152. end
  153.  
  154. -- ## EVENTS ## --
  155.  
  156. Players.PlayerAdded:Connect(function(player: Player)
  157. Backpacks[player] = {}
  158.  
  159. local backpack = player:FindFirstChildOfClass("Backpack") :: Backpack
  160. local backpackTable = Backpacks[player]
  161.  
  162. local equippedTool = nil
  163. local backpackCopy = Instance.new("Folder")
  164.  
  165. local connections = {}
  166.  
  167. backpackCopy.Name = tostring(player.UserId)
  168. backpackCopy.Parent = BackpacksFolder
  169.  
  170. player.CharacterAdded:Connect(function(character)
  171. task.defer(function()
  172. fillBackpack(player)
  173. end)
  174.  
  175. backpack = player:FindFirstChildOfClass("Backpack")
  176.  
  177. player.CharacterRemoving:Once(function()
  178. for _, connection in connections do
  179. connection:Disconnect()
  180. end
  181. end)
  182.  
  183. connections[#connections + 1] = backpack.ChildAdded:Connect(function(tool: Instance)
  184. if not tool:IsA("Tool") then
  185. return
  186. end
  187.  
  188. if not tool:GetAttribute("Id") then
  189. tool:SetAttribute("Id", HttpService:GenerateGUID(false))
  190. end
  191.  
  192. createClone(player, tool :: Tool)
  193. table.insert(backpackTable, tool)
  194. end)
  195.  
  196. connections[#connections + 1] = backpack.ChildRemoved:Connect(function(tool: Instance)
  197. if not tool:IsA("Tool") then
  198. return
  199. elseif player.Character and tool.Parent == player.Character then
  200. equippedTool = tool
  201. return
  202. end
  203.  
  204. deleteClone(player, tool :: Tool)
  205. table.remove(backpackTable, table.find(backpackTable, tool))
  206. end)
  207.  
  208. connections[#connections + 1] = character.ChildRemoved:Connect(function(child)
  209. if child ~= equippedTool then
  210. return
  211. end
  212.  
  213. equippedTool = nil
  214.  
  215. if child.Parent ~= backpack then
  216. deleteClone(player, child :: Tool)
  217. table.remove(backpackTable, table.find(backpackTable, child))
  218. end
  219. end)
  220. end)
  221. end)
  222.  
  223. Players.PlayerRemoving:Connect(function(player: Player)
  224. Backpacks[player] = nil
  225. end)
  226.  
  227. InvRem.ReorderInventory.OnServerEvent:Connect(function(player: Player, inventory: Inventory)
  228. if not validatePlayerInventory(player, inventory) then
  229. return
  230. end
  231.  
  232. print(inventory)
  233.  
  234. local newTable = {}
  235.  
  236. for index, tool in inventory do
  237. index = tonumber(index)
  238.  
  239. tool:SetAttribute("Position", index)
  240. createClone(player, tool)
  241.  
  242. newTable[index] = tool
  243. end
  244.  
  245. Backpacks[player] = newTable
  246. end)
  247.  
  248. -- ## SETUP ## --
  249.  
  250. for _, tool in ServerStorage.Tools:GetChildren() do
  251. if not tool:IsA("Tool") then
  252. continue
  253. end
  254.  
  255. IdToTool[tool:GetAttribute("Id") or tool.Name] = tool
  256. end
  257.  
  258. -- ## TYPES ## --
  259.  
  260. type Inventory = { [number]: Tool }
  261. type EncodedInventory = { [number]: string }
Advertisement
Add Comment
Please, Sign In to add comment