Roblox removes what you Destroy — until a reference quietly keeps it alive. Meet the three classic leaks and learn the habits that prevent them.
Optimization taught you to measure, then to clean up — destroyed, throttled, listeners disconnected. This lesson is about the leak in that plan: the thing you were sure you cleaned up, quietly staying behind. Games don't crash from leaks — they wear down. The memory number creeps up, frames get heavier, and after an hour of play the game feels tired. A memory leak is the name for it, and on Roblox there are only a few ways to make one. All of them share the same signature: a reference you never let go.
Luau's garbage collector frees anything that has no references left — no , no entry, no connection pointing at it. That's the whole rule. So a leak is never "memory doing something wrong." It's you (or a you wrote) still holding a reference to something you're done with.
On Roblox, the two things people hold onto by accident are:
The neat : Roblox hands you the measuring tool for free. In Studio Stats, the memory number goes up as you create things and comes back down as the collector frees them. Watch it while you play. Memory that climbs forever — even after your effects finish — is a leak. Memory that climbs and settles is just the game breathing.
A coin part was Destroy()-ed. Which situation still keeps it alive in memory?
The classic: a makes an for a moment of effect — confetti, an explosion spark, a spawn ring — and never removes it. It was fun, it's over, but every copy is still tracked:
local Debris = game:GetService("Debris")
clickEvent.OnServerEvent:Connect(function(player)
local wedge = Instance.new("WedgePart")
wedge.Size = Vector3.new(1, 1, 1)
wedge.Position = player.Character.Head.Position
wedge.Parent = workspace.Effects
end)
Every click drops a wedge into the world and nothing ever takes one out. Five minutes of clicking is a stack of debris the engine keeps tracking.
What's the bug here?
Roblox has a service built exactly for this — Debris. Hand it an and a delay, and it removes the from the world after that delay:
local Debris = game:GetService("Debris")
clickEvent.OnServerEvent:Connect(function(player)
local wedge = Instance.new("WedgePart")
wedge.Size = Vector3.new(1, 1, 1)
wedge.Position = player.Character.Head.Position
wedge.Parent = workspace.Effects
Debris:AddItem(wedge, 5) -- scheduled cleanup: gone in 5 seconds
end)
Debris removes an from the world — it can't free your references. If a or a still holds the , it stays alive even after Debris cleans it up. Destroy clears the world; dropping the last reference clears the memory. Both, together, are a clean exit.
Listeners are memory too — each connection stores its , its target, and a spot in the 's bookkeeping. So a connection created again and again is a leak even when each one is "tiny."
The classic victim is a that reconnects on every respawn or every activation:
-- every round, this coin activates — and gets ANOTHER listener
local function activateCoin(coin)
coin.Touched:Connect(function(hit)
collectCoin(hit)
end)
end
Each activation stacks another listener on top of the old ones. By round fifty that coin has fifty listeners — every one of them firing, every one of them stored.
After ten activations, how many collectCoin listeners does the coin have?
The rule from the optimization lesson, applied to time: connect once, or disconnect before reconnecting. When the coin deactivates, Disconnect its listener; when it activates again, it starts clean:
local coinConnection
local function deactivateCoin(coin)
if coinConnection then
coinConnection:Disconnect()
coinConnection = nil
end
end
local function activateCoin(coin)
deactivateCoin(coin) -- clear last round's listener first
coinConnection = coin.Touched:Connect(function(hit)
collectCoin(hit)
end)
end
The quietest leak is a that only ever grows. Trackers are everywhere — "all active coins", "effects for X", "recent hits" — and the natural code adds on spawn and forgets to remove on done:
local activeCoins = {}
local function spawnCoin()
table.insert(activeCoins, makeCoin())
end
-- ...and on collect? Nothing removes the coin from the list.
Now the list isn't just wasting space: it's the reference that keeps dead coins alive (remember Leak 1's note). The fix is to treat the list like the world — remove what leaves:
local function collectCoin(coin)
for i, tracked in ipairs(activeCoins) do
if tracked == coin then
table.remove(activeCoins, i)
break
end
end
coin:Destroy()
end
Per- bookkeeping has the same shape: you add an entry when a joins. If nothing ever removes it, a game running through hundreds of joins leaks an entry per join — and every entry can hold dead effects too.
Roblox does clean up a big share of stuff for you: the character, the PlayerGui, the sounds — all children of the go away on leave. What it can't clean is your . Those you release by hand:
local playerEffects = {}
game.Players.PlayerAdded:Connect(function(player)
playerEffects[player.UserId] = makeEffectsFor(player)
end)
game.Players.PlayerRemoving:Connect(function(player)
local effects = playerEffects[player.UserId]
if effects then
effects:Destroy()
playerEffects[player.UserId] = nil -- drop the whole entry
end
end)
Destroy the effects, then set the key to nil — that releases the entry to the collector. A that only ever grows is a leak you can watch coming.
In onPlayerLeave, write the line that releases the player's whole entry so the table can't grow with every join.
game.Players.PlayerRemoving:Connect(function(player)
playerEffects[player.UserId]:Destroy()
-- drop the entry itself so it doesn't stay in the table
end)Leaks are invisible until you measure — same rule as the optimization lesson. Open Studio Stats and play your game for a few minutes:
The three habits that close all three leaks:
Debris or Destroy at birth.Don't leak-hunt by reading. Find a real symptom first — the memory number that never settles — then hunt the list, connection, or creation site feeding it. Most memory goes back home by itself; the leak is the one thing that doesn't.
Next: Refactoring — reading a messy , spotting the smells, and restructuring it in small safe steps without changing a single pixel of gameplay.