local d={}local D={}D.__index=D function D.GetInstance(D,d)if D.InstancePoolsByClass[d]==nil then D.InstancePoolsByClass[d]={}end local P=#D.InstancePoolsByClass[d]if P>0 then local q=D.InstancePoolsByClass[d][P]table.remove(D.InstancePoolsByClass[d])return q end return Instance.new(d)end function D.ReturnInstance(D,d)if D.InstancePoolsByClass[d.ClassName]==nil then D.InstancePoolsByClass[d.ClassName]={}end if#D.InstancePoolsByClass[d.ClassName]<D.PoolSizePerType then table.insert(D.InstancePoolsByClass[d.ClassName],d)else d:Destroy()end end function d.new(d)local P=setmetatable({},D)P.InstancePoolsByClass={}P.Name="\079\098\106\101\099\116\080\111\111\108"P.PoolSizePerType=d return P end return d
--	// FileName: ObjectPool.lua
--	// Written by: TheGamer101
--	// Description: An object pool class used to avoid unnecessarily instantiating Instances.

local module = {}
--////////////////////////////// Include
--//////////////////////////////////////

--////////////////////////////// Methods
--//////////////////////////////////////
local methods = {}
methods.__index = methods

function methods:GetInstance(className)
  if self.InstancePoolsByClass[className] == nil then
    self.InstancePoolsByClass[className] = {}
  end
  local availableInstances = #self.InstancePoolsByClass[className]
  if availableInstances > 0 then
    local instance = self.InstancePoolsByClass[className][availableInstances]
    table.remove(self.InstancePoolsByClass[className])
    return instance
  end
  return Instance.new(className)
end

function methods:ReturnInstance(instance)
  if self.InstancePoolsByClass[instance.ClassName] == nil then
    self.InstancePoolsByClass[instance.ClassName] = {}
  end
  if #self.InstancePoolsByClass[instance.ClassName] < self.PoolSizePerType then
    table.insert(self.InstancePoolsByClass[instance.ClassName], instance)
  else
    instance:Destroy()
  end
end

--///////////////////////// Constructors
--//////////////////////////////////////

function module.new(poolSizePerType)
	local obj = setmetatable({}, methods)
	obj.InstancePoolsByClass = {}
	obj.Name = "ObjectPool"
  obj.PoolSizePerType = poolSizePerType

	return obj
end

return module
