How can I have client-side communication with object pools?

  1. I’m working out with a object pool here made by CoodSlayer (Kudos to him)
local ObjectPool = {}
ObjectPool.__index = ObjectPool

type ObjectPoolCallback = (object: Instance) -> ()

local function empty() end

function ObjectPool.new(object: Instance, size: number, onCreate: ObjectPoolCallback, onGet: ObjectPoolCallback, onFree: ObjectPoolCallback)
	local self = setmetatable({}, ObjectPool)

	self._objects = {}
	self._object = object
	self._onGet = onGet or empty
	self._onFree = onFree or empty
	onCreate = onCreate or empty

	for i = 1, size do
		local o = object:Clone()
		table.insert(self._objects, o)
		onCreate(o)
	end

	return self
end

function ObjectPool:get()
	if #self._objects == 0 then
		return self._object:Clone()
	end

	local object = table.remove(self._objects, 1)
	self._onGet(object)
	return object
end

function ObjectPool:free(object: Instance)
	self._onFree(object)
	table.insert(self._objects, object)
end

function ObjectPool:clear()
	for _, object in ipairs(self._objects) do
		object:Destroy()
	end
	self._objects = {}
end

return ObjectPool

I’m trying to work something out with some projectile system, where instead of utilizing the usual clone on client replication, I want it to access the objectpool from the component, get an object, and simply do clone but faster. I really can’t figure anything out. Code below.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

--component definition
local Component = {}
Component.__index = Component

--private variables
local typeConfig = require(ReplicatedStorage.Data.Packets.Types.ProjectilePacket.Projectile)
local objectPool = require(ReplicatedStorage.Modules.Loader.Util.ObjectPool)

local localPlayer = Players.LocalPlayer
local mouse = localPlayer:GetMouse()

local ev_CreateProjectile = ReplicatedStorage.Event.Client.CreateProjectile

--private funtions

--component functions (may god bless me on the dev forums so many people are 100% going to think im using ai soley because of comments this has already happened TWICE before if it happens a third time ill actually lose it)

function Component.new(instance: Tool)
	local c = setmetatable({}, Component)
	c.instance = instance
	c._initialized = false
	
	local Type = "Grenade"
	c.type = Type
	
	
	c:_init()
	return c
end

function Component:_init()
	if self._initialized == true then return end
	self._initialized = true
	
	local debounceTime = 0
	local debounce = false
	
	local Type = self.type
	
	self.instance.Activated:Connect(function()
		if not debounce then
			debounce = true
			local handlePosition = self.instance.Handle.Position
			local mouseHit = mouse.Hit.Position
			local direction : Vector3 = (mouseHit - handlePosition).Unit

			ev_CreateProjectile:Fire(Type, handlePosition, direction)
			
			task.wait(debounceTime)
			debounce = false
		end
	end)
end

return Component

Client Replication System

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local module = {}

local ProjectileCache = {}

local sharedType = require(ReplicatedStorage.Data.Packets.Types.ProjectilePacket.SharedType)
local sharedConfig = require(ReplicatedStorage.Data.Packets.Config.MaterialPacket.SharedConfig)
local projectiles = require(ReplicatedStorage.Data.Packets.Types.ProjectilePacket.Projectile)
local localPlayer = Players.LocalPlayer

local rev_ReplicateProjectile = ReplicatedStorage.Event.Remote.ReplicateProjectile
local ev_CreateProjectile = ReplicatedStorage.Event.Client.CreateProjectile

local CastParams = RaycastParams.new()
CastParams.FilterType = Enum.RaycastFilterType.Exclude


--helper

function time_source() : number
	return workspace:GetServerTimeNow()
end

function stepProjectile(t, dt)
	for visual, projectile : sharedType.ProjectilePacket in pairs(ProjectileCache) do
		local timeNow = time_source()
		local projectileInfo = projectiles[projectile.ProjectileType]
		CastParams.FilterDescendantsInstances = {localPlayer.Character, workspace.Debris}

		projectile.Bounces = projectile.Bounces or 0

		local steppedVelocity = projectile.Velocity + (projectile.Acceleration * dt)
		local nextPosition = projectile.Position + (steppedVelocity * dt)

		local start = projectile.Position
		local direction = nextPosition - start

		local minRay = 0.1
		if direction.Magnitude < minRay then
			direction = direction.Unit * minRay
		end

		local found = false
		local castresult

		local castResult = workspace:Raycast(start, direction, CastParams)
		if castResult then
			projectile.Bounces += 1
			projectile.Position = castResult.Position

			if projectile.Bounces < projectile.MaxBounces then
				projectile.Velocity = projectile.Decay * (steppedVelocity - 2 * steppedVelocity:Dot(castResult.Normal) * castResult.Normal)
				if projectile.Velocity.Magnitude < projectile.Treshold then
					projectile.Velocity = Vector3.zero
					projectile.Acceleration = Vector3.zero
					projectile.Position = castResult.Position
				end
			else
				found = true
			end
		elseif timeNow - projectile.StartTime > projectileInfo.Lifetime then
			found = true
		else
			projectile.Position = nextPosition
			projectile.Velocity = steppedVelocity
		end

		visual.CFrame = CFrame.new(projectile.Position, projectile.Position + projectile.Velocity)

		if found then
			projectile.ClientCallback(visual, castResult, dt, timeNow)
			ProjectileCache[visual] = nil
			visual:Destroy()
		end
	end
end


function addProjectile(id : string, start : Vector3, direction : Vector3)
	local data = projectiles[id]
	local t0 = time_source()
	
	local new : sharedType.ProjectilePacket = {
		Id = id;
		Origin = start;
		Position = start;
		Velocity = direction * data.Velocity;
		Acceleration = data.Acceleration;
		MaxBounces = data.MaxBounces;
		Decay = data.Decay;
		Treshold = data.Treshold;
		StartTime = t0;
		ProjectileType = id;
		ClientCallback = data.ClientCallback;
		ServerCallback = data.ServerCallback;
	}
	
	rev_ReplicateProjectile:FireServer(new)
	
	local visual = data.Model:Clone()
	visual.CFrame = CFrame.new(start, start + direction)
	visual.Parent = workspace.Debris
	new.Visual = data.Model:Clone();
	ProjectileCache[visual] = new
end

function replicateProjectile(packet : sharedType.ProjectilePacket)
	local data = projectiles[packet.Id]
	local currentTime = time_source()
	local deltaTime = currentTime - packet.StartTime
	local visual = data.Model:Clone()
	visual.Parent = workspace.Debris
	packet.Position = packet.Origin + (packet.Gravity * deltaTime)
	ProjectileCache[visual] = packet
end

--module start

local started = false

function module.Start()
	if started then return end
	started = true
	
	RunService.Stepped:Connect(stepProjectile)
	ev_CreateProjectile.Event:Connect(addProjectile)
	rev_ReplicateProjectile.OnClientEvent:Connect(replicateProjectile)
end

return module

Followed qweekers tutorial for a portion (portion as in like almost all of it) for this part of the system

may god save my portfolio im NOT getting hired Bru

not quite sure i understand the question; by ‘client-side communication with object pools’, do you mean pooling objects on the client? attaching client logic to objects taken out of pools on the server?

Pooling from a object pool on an different script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local objectPool = require(ReplicatedStorage.Modules.Loader.Util.ObjectPool)

local poolCache = {}
local module = {}

function module.Register(name, ...)
	local pool = objectPool.new(...)
	poolCache[name] = pool
end

function module.Get(name)
	local pool = poolCache[name]
	assert(pool, "Object pool not found: " .. name)
	return pool:get()
end

function module.Free(name, object: Instance)
	local pool = poolCache[name]
	assert(pool, "Object pool not found: " .. name)
	return pool:free(object)
end

function module.Clear(name)
	local pool = poolCache[name]
	pool:clear()
	poolCache[name] = nil
end

return module

I’ve figured how to work something out, and I’ve done it with a service thingamajig
Is this right