Advertisement
InTesting

Caching Instances

Sep 21st, 2021
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.79 KB | None | 0 0
  1. local cache = {}
  2.  
  3. --[[
  4.     cache = {
  5.         className = {
  6.             instance1,
  7.             instance2
  8.            
  9.             ...
  10.         }
  11.     }
  12. ]]
  13.  
  14. -- instance.new
  15. function getInstance(className, parent)
  16.     local ins
  17.    
  18.     if cache[className]then
  19.         ins = cache[className][1]
  20.         if ins then
  21.             table.remove(cache[className],1)
  22.         end
  23.     else
  24.         cache[className] = {}
  25.     end
  26.    
  27.     if not ins then
  28.         ins = Instance.new(className)
  29.     end
  30.    
  31.     if parent then
  32.         ins.Parent = parent
  33.     end
  34.    
  35.     return ins
  36. end
  37.  
  38. -- destroy
  39. function cacheInstance(ins)
  40.     local className = ins.ClassName
  41.    
  42.     cache[className] = cache[className] or {}
  43.    
  44.     table.insert(cache[className])
  45.    
  46.     ins.Parent = nil
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement