network/touchReplication huge memory leak

In a game I’m working on, I have a lot of parts created on the server, with the touch events handled on the client. This however has led to massive (2,000+ MB) amounts of memory consumption on the server after just a few hours, and I was wondering what might be causing that and if there’s any way to stop it. To give a basic example of how the code is being used:

Server Script
-----Server-----
hitFunctions = {
    X = function(hit)
        ...
    end
}

while true do
    wait(1)
    local part = PartArray[*random part*]:Clone()
    game.Debris:AddItem(part, 600)
    if part.Name == "X" then 
        part.Touched:Connect(function(hit) 
            hitFunctions[Part.Name](hit) 
            part:Destroy()
        end) 
    end
    part.Parent = game.Workspace
end

RandomRemote.OnServerEvent:Connect(function(plr, part)
    RandomRemote:FireAllClients(plr, part)
    part:Destroy()
end)
LocalScript
------LocalScript--------
hitFunctions = {
    x = function(hit)
        RandomRemote:FireServer(hit)
        --fire event to the server telling it the player hit that part, server then deleted that part
    end
}

game.Workspace.ChildAdded:Connect(function(part)
    if part.Name == "x" then
        part.Touched:Connect(function(hit) 
            hitFunctions[part.Name](hit) 
        end)
    end
end)

To give more detail:
-The client and server never have touch events on the same part (either handled by client or server)
-The maximum number of existing parts is 300, and a new one is only created when one is destroyed

you’re creating 1 part per second.
3600 parts an hour
Presuming all have the same name ‘X’ - that’s a lot of parts with touch events on the server…

That was just an example to show it constantly looping, realistically it’s doing maybe 100/hour, and the maximum part count is ~300 at any given time. Parts are only created when one is destroyed

Can you provide an example of what exactly is interacting with these instantiated parts?

For example, if it is the Player - can you simply move your Touch event to the player’s RootPart and handle the interaction when a touch occurs there instead?
(instead of 300 touch events, just 1?)

It’s pretty hard to understand your problem as I believe this isn’t your main code, and that leads to some complications. In the server script, you’re duplicating parts but checking if the name is X when it should always be X since it’s the same part. However, you are creating multiple connections which are only disconnected once the part is destroyed meaning if they are never destroyed the parts connection is never severed, causing some memory leaks. Not to mention the amount of just stray parts which build up over time.

I’d also like to add that you don’t have to do your client side line and can I believe you may be able to just do this:

part.Touched:Connect(hitFunctions[part.Name])

The things interacting with the parts are the player’s character and parts that the server created but the collisions of which are handled locally

But as long as the part is destroyed the connections are garbage collected right? I’ll update the code to give a better example; I hate typing in the editor here

Edit: code updated

Do you have any debounces in your touch events?
So the parts are not firing off hundreds of times per second depending on interaction?

Are they also filtered to only intereact with a player and not each other?

Yeah, the client sends the touch event to the server a maximum of one time, and the created Parts are all anchored so they only ever really interact with the player.

Are you disconnecting those Touch events after anchoring?

Also, in regards to network ownership - remember that anchoring or disabling collisions on the client will not replicate to the server - you still need to disconnect those events on the server.

What do you mean by disconnecting them after anchoring?
The spawned Parts are anchored to begin with; I’ll create a demo place really quickly

Ah, as I’ve found out, the issue may be in another thing instead (same issue of creating parts on the server + detecting collisions on the client except 100x the number of collisions)

You should add a line of code which validates that the touching part belongs to a player’s character before further executing the remainder of the function.