Question regarding garbage collection

In my game, there’re parts that will kill the players, however, I want to destroy these bricks later on. But inside the script, there is a table called debounces (basically cooldown). Would it get garbage collect the table debounces in this code after the bricks have been destroyed and connections are disconnected?

local Players = game:GetService("Players")
local debounces = {}

part.Touched:Connect(function(hit)
  local plr = Players:GetPlayerFromCharacter(hit.Parent)
  if plr and not debounces[plr] then
    debounces[plr] = true
    hit.Parent.Humanoid.Health = 0  
    wait(1)
    debounces[plr] = false
  end
end)
1 Like

When the part is destroyed the Touched connection is automatically disconnected, and at that point the anonymous function goes out of scope so there are no references to it, letting it be GC’ed. Once the anon function gets GC’ed there will be no references to debounces, causing it to be GC’ed in turn when it goes out of scope at the end of the script.

See this post for the situations where you need to be careful about memory leaks.