In an Object Pool System, model contents randomly disappear, difficult to find the cause

I’m trying to write an Object Pool system that can spawn collectable objects (5-80 at a time), when collected they are stored in a pool to be reused later, if they are not collected they despawn into the pool after some time period.

As a heads-up, I believe this is an advanced problem, or an issue with Roblox implementation details (hidden to us), and not a beginner scripting issue (unless I’ve somehow missed something obvious). So I’m hoping some more experienced Roblox developers will have an idea of what could be going wrong.

How the system is setup:

  • The items are being managed by a module that is used as a collection of static functions, not as an instanced object
  • A pool folder is prepared in ServerStorage, and an active folder is prepared in Workspace
  • When an item is requested, it first checks the pool, and if there are items in the pool (GetChildren()) it will take from the end of the list of children
  • If there are no items in the pool, a template item is cloned
  • When an item is requested, a task to despawn the item is setup (task.delay())
  • The despawn threads are tracked in the module inside a table (threadTable = { itemObject = despawnThread})
  • If the despawn delay runs out, a function to release the item back into the pool is called
  • If the item is collected, the same release function is called
  • The release function checks the threadTable to see if any threads are “suspended” and calls task.cancel() on them if so. (This thread cancelling code is run in a task.defer(), because the thread calls the same release function)
  • Spawned items are Parented into Workspace, released items are Parented into ServerStorage

The issues are

  • Somewhere in the cycle, model contents are disappearing (or are never cloning)
  • The issue seems to happen randomly, the server may be stable for 5-10 mins before the issue occurs (this makes the problem really difficult to track)

I’ve looked for solutions to this problem (and another related one) for a few days. It’s not an obvious problem because I believe it has something to do with how Studio/Roblox works. I’ve temporarily written some workarounds, I check if the item model has a PrimaryPart, if it doesn’t I’ll dispose the model and clone a new one. I also have a few checks for the item model PrimaryPart, and bypass code that depends on it). Obviously I don’t want to do this and actually find a stable solution.

A few hints:

  • The models still appear in the pool, but have no contents (which causes errors when I look for the contents)
  • Sometimes the models have a large -y coordinate (i.e. negative millions/billions, which shouldn’t be happening), this may suggest the item is somehow falling inside ServerStorage (even if anchored). However, I have also seen models without contents at a reasonable y coordinate too.

Some details related to my specific implementation:

  • The game is a space game, and a LinearVelocity is used to make the items float in space
  • Debouncing is handled across multiple items with item:SetAttribute("IsHit", true) and item:GetAttribute("IsHit")
  • The items, when spawned, have item.PrimaryPart:SetNetworkOwnership(player) on them because the Touch Event and collection was really laggy when handled on Server (actually when I changed the Network Ownership to the player, the debouncing stopped working properly – which is an additional issue)
  • I am not currently using Streaming
  • Released items are anchored while in ServerStorage
  • The workspace fallen parts destroy height is set to -5000, and the lowest spawning should be about -1000

Here’s an image showing the Pool in ServerStorage, with most items having contents and a few with missing contents. You can get an idea for what the contents are, and you can see some of the attributes, and you can see the bugged items have a really massive -y value:

Any ideas as to why this is happening? Why the spawn/release cycle can happen multiple times without issue and then the issue happens randomly? Is there an issue with using ServerStorage in this way for quickly spawning/releasing many items (e.g. ~80) at once?

Thank you for your time!

I’ve tried to create a test place to reproduce the bug, and I’ve replicated more or less the same code and situation, but I cannot reproduce the bug yet even after running for 25+ mins.

I’ll keep trying to reproduce the bug, and share the test place if I can.

Can anyone think of any scenario in which a model can keep its container and lose its contents? Other than manually destroying the contents?

Thanks