Server getting laggier the longer it runs? A memory leak checklist

If your world runs fine for the first 10 minutes and then gets laggier the longer a server stays up, a script-side memory leak is one of the first things worth ruling out. Here’s the checklist I’d go through first.

Quick background on why this happens: Lua’s GC only collects things that nothing references anymore. “I called Destroy()” doesn’t mean the memory is gone if some table or connection still points at it.

1. Tables keyed by player that never get cleared

Super common pattern:

local cooldowns = {}
local playerStats = {}

Entries get added on join, and nobody removes them on leave. On a long-running server with people coming and going, that grows forever. Every per-player table needs a matching cleanup:

Players.PlayerRemoving:Connect(function(player)
    cooldowns[player] = nil
    playerStats[player] = nil
end)

2. Connections that are never disconnected

The docs specifically call out events tied to players and characters. If you connect something per player or per character, keep the connection and disconnect it when you’re done:

local Players = game:GetService("Players")

local connections = {} -- [player] = { connection, ... }

local function track(player, connection)
    if not connections[player] then
        connections[player] = {}
    end
    table.insert(connections[player], connection)
end

Players.PlayerAdded:Connect(function(player)
    track(player, player.CharacterAdded:Connect(function(character)
        -- per-character setup
    end))
end)

Players.PlayerRemoving:Connect(function(player)
    local list = connections[player]
    if list then
        for _, connection in ipairs(list) do
            connection:Disconnect()
        end
    end
    connections[player] = nil
end)

Same goes for anything frame-based like Heartbeat. Connect it only while you need it, disconnect it after.

3. Destroy() without nil

effect:Destroy()
effect = nil

If effect is an upvalue or lives in a long-lived table, the Destroy alone isn’t enough for the GC to take it.

4. Creating and destroying lots of instances at runtime

Bullets, hit effects, damage numbers. Creating a new one with Instance.new() or Clone() every time and destroying it after means a lot of heap churn, and more heap objects means GC runs more often, which shows up as frame drops. A simple pool (create N up front, mark them active/inactive, reuse) fixes most of it. The docs have a full BulletManager pooling example if you want something to start from.

Related: don’t spawn 50 enemies in the same frame. Spread them out over a few frames.

5. One script per object

If you have 40 kill parts each with their own script, that’s 40 sets of connections doing the same thing. One manager script that handles all of them is easier on performance and much easier to change later.

None of this is exotic, but it’s worth ruling these five out before looking for something deeper.

Source: Practical Guide to Script Optimization

Anyone have a good workflow for actually spotting which of these it is? Curious how people narrow it down before going through every script by hand.