Advertisement
RuizuKun_Dev

Debris2

Aug 7th, 2020
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.14 KB | None | 0 0
  1. --[[
  2.     @game/ReplicatedStorage/Debris2
  3.         Debris2 = {
  4.             Instances {
  5.                 [Debris: instance or table or RBXScriptConnection] = {
  6.                     lifeTime = lifeTime: number,
  7.                     removalTime = tick() + lifeTime: number,
  8.                     Destroyed = nil, -- Destroyed: callback Function | nil by default
  9.                     Cancel = function -- remove references and disconnect Hearbeat
  10.                     Instance = item: (Instance or table or RBXScriptConnection),
  11.                 },
  12.             },
  13.         }
  14.        
  15.         -- Methods
  16.         AddItem (item: Instance or table or RBXScriptConnection, lifeTime: number?) -> Debris
  17.         AddItems (arrayOfItems: {Instance, table, RBXScriptConnection}, lifeTime: number?) -> void
  18.         GetAllDebris () -> Instances
  19.         GetDebris (item: Instance or table or RBXScriptConnection) -> Debris
  20. --]]
  21.  
  22. local Debris2 = {
  23.     Instances = {}
  24. }
  25.  
  26. --| SERVICES:
  27. local RunS = game:GetService("RunService")
  28.     local Heatbeat = RunS.Heartbeat
  29.  
  30. --| MODULES:
  31.  
  32. --| VARIABLES:
  33.  
  34. local Instances = Debris2.Instances
  35.  
  36. --| TABLES:
  37.  
  38. local Connections = {}
  39.  
  40. local ValidTypes = {
  41.     ["Instance"] = "Destroy",
  42.     ["table"] = true,
  43.     ["RBXScriptConnection"] = "Disconnect",
  44. }
  45.  
  46. local METHODS = { -- add any Custom Destroy/Remove/Clear/CleanUp methods here
  47.     "Destroy",
  48.     "Disconnect",
  49.    
  50.     "destroy",
  51.     "disconnect",
  52. }
  53.  
  54. --| META TABLES:
  55.  
  56. --| FUNCTIONS:
  57.  
  58. local function removeItem(typeOf, object)
  59.     if typeOf == "Instance" then
  60.         pcall(object.Destroy, object)
  61.     elseif typeOf == "RBXScriptConnection" then
  62.         pcall(object.Disconnect, object)
  63.     else
  64.         for _,v in ipairs(METHODS) do -- _, v: method name
  65.             if object[v] then
  66.                 pcall(object[v], v)
  67.                 break
  68.             end
  69.         end
  70.     end
  71. end
  72.  
  73. local function addDebris(object, lifeTime)
  74.    
  75.     local typeOf = typeof(object)
  76.    
  77.     assert(ValidTypes[typeof(object)])
  78.     assert(typeof(lifeTime) == "number")
  79.    
  80.     if (not Instances[object]) then
  81.         table.insert(Instances, object)
  82.     end
  83.    
  84.     Instances[object] = {
  85.         ["lifeTime"] = lifeTime,
  86.         removalTime = tick() + lifeTime,
  87. --      Destroyed = nil, -- Destroyed: callback Function
  88.         Cancel = function() -- remove references and disconnect Hearbeat
  89.             Connections[object]:Disconnect()
  90.             table.remove(Instances,table.find(Instances, object))
  91.             Instances[object] = nil
  92.         end,
  93.         ["Instance"] = object,
  94.     }
  95.    
  96.     local debris = Instances[object]
  97.    
  98.     Connections[object] = Heatbeat:Connect(function()
  99.         if debris and tick() >= debris.removalTime then
  100.             if debris.Destroyed then
  101.                 debris.Destroyed()
  102.             end
  103.             debris.Cancel()
  104.             removeItem(typeOf,object)
  105.         end
  106.         debris = Instances[object]
  107.     end)
  108.    
  109.     return Instances[object]
  110. end
  111.  
  112. --| METHODS:
  113.  
  114. function Debris2:AddItem(item, lifeTime) -- item: (Instance, table, RBXScriptConnection), lifeTime: number
  115.     return addDebris(item, lifeTime)
  116. end
  117.  
  118. function Debris2:AddItems(arrayOfItems, lifeTime) -- arrayOfItems: (Instance, table, RBXScriptConnection), lifeTime: number
  119.     for _,item in ipairs(arrayOfItems) do
  120.         addDebris(item, lifeTime)
  121.     end
  122. end
  123.  
  124. function Debris2:GetAllDebris()
  125.     return Instances
  126. end
  127. Debris2.getAllDebris = Debris2.GetAllDebris
  128.  
  129. function Debris2:GetDebris(item)
  130.     return Instances[item]
  131. end
  132. Debris2.getDebris = Debris2.GetDebris
  133.  
  134. --| SCRIPTS:
  135.  
  136. -- return:
  137. return Debris2
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement